简体   繁体   中英

how to select all records from one table and some from another table in cakephp 3.6

SELECT myTable.*, otherTable.foo, otherTable.bar...

how can we write above query in cakephp ? I tried this but didn't work.

$data = $this->Articles->find()->select(['Articles.*','Categories.name'])->innerJoineWith('Categories');

It giving me error near SELECT Fees.* AS Fees__* .

So instead of that, I have to write all columns of the Article Table.

$data = $this->Articles->find()->select(['Articles.id','Articles.name','Articles.title','Articles.description','Categories.name'])->innerJoineWith('Categories');

is there any solution in cakephp? please tell me. Thank You.

You can do like this:

$this->Articles->find('all')->contain(['Categories' => function($q) {
                    return $q->select('Categories.name');
                }])->select($this->Articles);

$this->Articles in select statement will fetch all the records from the Articles table and $q->select('Categories.name') will fetch only Category name from the associated Categories table.

Ref: https://github.com/cakephp/cakephp/issues/7913

$data = $this->Articles->find()
        ->select($this->Articles)
        ->select(['Categories.name'])
        ->innerJoineWith('Categories');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM