query-builder

How to limit contained associations per record/group?

一笑奈何 提交于 2019-11-26 13:44:21
I have a Model, Articles, which hasMany Abstracts. I want to load the 10 latest Articles, and for each Article, the Abstract with the highest number of points. My function looks like this: public function getArticles($category, $viewName) { $subArticles = $this->Articles->findByCategory($category)->contain([ 'Abstracts' => function ($q) { return $q ->select(['body', 'points', 'article_id']) ->where(['Abstracts.approved' => true]) ->limit(10) ->order(['Abstracts.points' => 'DESC']); } ]) ->limit(10) ->order(['Articles.created' => 'DESC']) ; $this->set( $viewName . 'Articles', $subArticles ); }

How to safely use reserved SQL names?

纵饮孤独 提交于 2019-11-26 12:33:26
问题 I\'m using Cakephp 3 using sqlserver as datasource server. I am sure there\'s no problem with my database connection.. as home.ctp prompts that I am connected to my database.. and I\'m as well using migrations plugin to create my tables.. it seems like there is no problem working with these tools. but after I bake my MVC, I only got page full of errors.. for example $bin\\cake bake all tests there are no errors I found and MVC are in its specific folder, testController.php, testTable, etc.

How to select from subquery using Laravel Query Builder?

孤者浪人 提交于 2019-11-26 12:08:47
I'd like to get value by the following SQL using Eloquent ORM. - SQL SELECT COUNT(*) FROM (SELECT * FROM abc GROUP BY col1) AS a; Then I considered the following. - Code $sql = Abc::from('abc AS a')->groupBy('col1')->toSql(); $num = Abc::from(\DB::raw($sql))->count(); print $num; I'm looking for a better solution. Please tell me simplest solution. In addition to @delmadord's answer and your comments: Currently there is no method to create subquery in FROM clause, so you need to manually use raw statement, then, if necessary, you will merge all the bindings: $sub = Abc::where(..)->groupBy(..);

Select entries between dates in doctrine 2

眉间皱痕 提交于 2019-11-26 09:09:47
问题 I will go insane with this minimal error that I\'m not getting fix. I want to select entries between two days, the examples below ilustrate all my fails: opt 1. $qb->where(\'e.fecha > \' . $monday->format(\'Y-m-d\')); $qb->andWhere(\'e.fecha < \' . $sunday->format(\'Y-m-d\')); result (0 entries): SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2 FROM reservacion r0_ WHERE (r0_.fecha > 2012 - 07 - 16) AND (r0_.fecha < 2012 - 07 - 22) opt 2 $qb->add(\

Cakephp-3.x: How to change the data type of a selected alias?

独自空忆成欢 提交于 2019-11-26 08:38:16
问题 WHen i try to do : $fields = array(\'id\' => \'custom_id\', \'title\' => \'some_name\'); The result I get has id as a string. If I do: $fields = array(\'custom_id\', \'title\' => \'some_name\'); then it gives custom_id as integer. How can I obtain custom_id as id without loosing the data type. I read the documentation but didn\'t found much help. There is something that virtual fields can do I think. But is it possible inside the find query without the use of virtual fields etc? Thanks in

How to use WHERE IN with Doctrine 2

本秂侑毒 提交于 2019-11-26 07:58:26
问题 I have the following code which gives me the error: Message: Invalid parameter number: number of bound variables does not match number of tokens Code: public function getCount($ids, $outcome) { if (!is_array($ids)) { $ids = array($ids); } $qb = $this->getEntityManager()->createQueryBuilder(); $qb->add(\'select\', $qb->expr()->count(\'r.id\')) ->add(\'from\', \'\\My\\Entity\\Rating r\'); if ($outcome === \'wins\') { $qb->add(\'where\', $qb->expr()->in(\'r.winner\', array(\'?1\'))); } if (

How to limit contained associations per record/group?

岁酱吖の 提交于 2019-11-26 02:57:49
问题 I have a Model, Articles, which hasMany Abstracts. I want to load the 10 latest Articles, and for each Article, the Abstract with the highest number of points. My function looks like this: public function getArticles($category, $viewName) { $subArticles = $this->Articles->findByCategory($category)->contain([ \'Abstracts\' => function ($q) { return $q ->select([\'body\', \'points\', \'article_id\']) ->where([\'Abstracts.approved\' => true]) ->limit(10) ->order([\'Abstracts.points\' => \'DESC\'

How to select from subquery using Laravel Query Builder?

一世执手 提交于 2019-11-26 02:31:12
问题 I\'d like to get value by the following SQL using Eloquent ORM. - SQL SELECT COUNT(*) FROM (SELECT * FROM abc GROUP BY col1) AS a; Then I considered the following. - Code $sql = Abc::from(\'abc AS a\')->groupBy(\'col1\')->toSql(); $num = Abc::from(\\DB::raw($sql))->count(); print $num; I\'m looking for a better solution. Please tell me simplest solution. 回答1: In addition to @delmadord's answer and your comments: Currently there is no method to create subquery in FROM clause, so you need to

How to filter by conditions for associated models?

早过忘川 提交于 2019-11-26 01:01:51
问题 I have a belongsToMany association on Users and Contacts. I would like to find the Contacts of the given User. I would need something like $this->Contacts->find()->contain([\'Users\' => [\'Users.id\' => 1]]); The cookbook speaks about giving conditions to contain, custom finder methods and sing through association key, but I did not find out how to put these together. 回答1: Use Query::matching() or Query::innerJoinWith() When querying from the Contacts table, then what you are looking for is

How to filter by conditions for associated models?

若如初见. 提交于 2019-11-25 22:48:37
I have a belongsToMany association on Users and Contacts. I would like to find the Contacts of the given User. I would need something like $this->Contacts->find()->contain(['Users' => ['Users.id' => 1]]); The cookbook speaks about giving conditions to contain, custom finder methods and sing through association key, but I did not find out how to put these together. Use Query::matching() or Query::innerJoinWith() When querying from the Contacts table, then what you are looking for is Query::matching() or Query::innerJoinWith() , not (only) Query::contain() . See Cookbook > Database Access & ORM