1

I want to perform a custom query in zf2. Now I have a Album controller and AlbumTable. Inside AlbumTable, I want to perform an join operation. But I am unable to do this.Please give me some suggation.

below my code:

namespace WebApp\Table;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;

new Zend\Db\Adapter\Adapter;

class UserTable
{

   protected $tableGateway;

   public function __construct(TableGateway $tableGateway)
   {
       $this->tableGateway = $tableGateway;
   }

   public function searchUser($search)
   {

    $search    = "mehedi";
    $adapter   = new Adapter();
    $sql       = new Sql($adapter);
    $select    = $sql->select();
    $select->from('foo');
    $select->join('profiles', 'user.user_id = profiles.ownerId', array('name'));
    $select->where(array('id' => 2));
    $statement = $sql->prepareStatementForSqlObject($select);
    $results   = $statement->execute();
    return $results;
   }

}

3 Answers 3

1

The issue is you are trying to instantiate your Adapter with no parameters, when it requires at least a driver :

 $adapter   = new Adapter(); // Bad
 $adapter   = new Adapter($driver); // ..

You should use the ServiceManager to get your Adapter, did you start with the Skeleton Application?

It should have already been injected into the TableGateway for you..

$adapter = $this->getAdapter();

An example of instantiating an Adapter:

$config = $serviceLocator->get('Config');
$adapter = new Adapter($config['db']);

where you specify your setup inside your config, local.php will do:

return array(
    /**
     * Database Config
     */
    'db' => array(
        'driver'    => 'pdo',
        'dsn'       => 'mysql:dbname=dbname;host=localhost',
        'username'  => 'root',
        'password'  => 'password',
    ),
Sign up to request clarification or add additional context in comments.

2 Comments

in line $adapter = new Adapter($driver); how can i get $driver. Please can you give a brief.
Thans Andrew for your nice ans.
1

I got some solution of this problem with helping all of here.

    $adapter = $this->tableGateway->getAdapter();

   /* $adapter variable is used to fetch Adapter 
   configuration from serivce manager. */

    $sql       = new Sql($adapter);
    $select    = $sql->select();
    $select->from('foo');
    $select->join('profile', 'foo.skillId = profile.id', array('name'));

    $statement = $sql->prepareStatementForSqlObject($select);
    $results   = $statement->execute();

   /* if you want you can see your
   desired output in here. */

    foreach ($results as $person) {
        echo "<pre>";
        print_r($person);
    }

    return $results;

Comments

0

It's because the join method expects an array for the join table. Also I personally would prefix the tables in the query, something like this:-

 $search    = "mehedi";
 $adapter   = new Adapter();
 $sql       = new Sql($adapter);
 $select    = $sql->select();
 // PREFIXED THE foo table f
 $select->from(array('f' =>'foo'));
 // PREFIXED THE profiles table p 
 $select->join(array('p' => 'profiles'), 'user.user_id = profiles.ownerId', array('name'));
 $select->where(array('id' => 2));
 $statement = $sql->prepareStatementForSqlObject($select);
 $results   = $statement->execute();
 return $results;

1 Comment

Actually this issue is not completely related with Alias. You could do the same operation without alias. The main issue was unable to create an instance of Adapter. As all entity(table entity) must use tablegateway so $adapter = $this->tableGateway->getAdapter(); is the best approach to initialize an adapter instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.