query-builder

How to write select query with subquery using laravel Eloquent Querybuilder?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 22:52:20
问题 I had got the result from the query. My Simple SQL is: SELECT o2.driver_id, total_delieveries, DATE_FORMAT(o1.created_at ,'%Y-%m-%d') AS created_at FROM ( SELECT driver_id, created_at, COUNT(driver_id) AS total_delieveries FROM orders WHERE is_paid = 0 AND order_status = 5 AND created_at BETWEEN "'.$first_Day.'" AND "'.$last_Day.'" GROUP BY DATE_FORMAT(created_at ,'%Y-%m-%d'),driver_id ) o1 INNER JOIN orders o2 ON o1.driver_id = o2.driver_id GROUP BY o1.created_at In Laravel source, I wrote

Symfony form query_buider and entity repository

半腔热情 提交于 2019-11-29 21:54:41
I'm trying to create a form with data in collection type depending on the user being logged. I'm following this chapter of the Symfony cookbook . Everything works fine when the query_builder option is a closure where I get my data from DQL. As the data need to be fetched from different location in code, I would prefer to define the query in the Repository class. Here is the function in my repository : public function findOwnedBy($user) { $query = $this->getEntityManager()->createQuery("SELECT l FROM MyBundle:Article a JOIN a.owndBy u WHERE u.id = :userId"); $query->setParameters(array("userId"

How to match records that are associated with a specific set of other records?

若如初见. 提交于 2019-11-29 18:13:51
I am trying to add to two different search variations to my project. There is a model "User" and a model "Tag". A User has many Tags. Now I want to be able to search the Users with specific Tags. Either I want to show all Users that has any of the specified tags. I got this working this way: $query = $this->Users->find(); $query->matching('Tags', function ($q) { return $q->where(['Tags.name' => 'Tag1']) ->orWhere(['Tags.name' => 'Tag2']); }); But now I want to find all Users that have both Tags at the same time. I tried ->andWhere instead of ->orWhere , but the result is always empty. How can

How to select fields of a contained association?

前提是你 提交于 2019-11-29 12:44:01
I would like to execute the following query where I'd like to read only the necessary fields from associated. I used a dot notation in select() below to better explain what I want. Basically the select() seems to concern Users only. Is it possible to specify the fields of Sites ? $orders = $this->Orders->find() ->contain([ 'Sites.Users'=> function ($q) { return $q ->select([ 'Sites.id', 'Sites.user_id', 'Users.id', 'Users.name', 'Users.owner_id', 'Users.firstname', 'Users.lastname' ]) ->autoFields(false); }, ]) ->first(); You have to configure the containments individually, selecting fields

symfony2 form querybuilder with parameters

无人久伴 提交于 2019-11-29 10:59:03
问题 I want to put my entity in the function of the query builder: ->add( 'weeks', 'entity', array( 'class' => 'MV\CaravanBundle\Entity\CaravanRow', 'property' => 'line', 'query_builder' => function(EntityRepository $er ) use ( $caravan ) { return $er->createQueryBuilder('w') ->orderBy('w.dateFrom', 'ASC') ->where('w.caravan = ?', $caravan ) ->andWhere('w.visible = 1') ->andWhere('w.booked = 0'); } but get the message: Expression of type 'Entity\Name' not allowed in this context So what is the

Laravel query builder - re-use query with amended where statement

大城市里の小女人 提交于 2019-11-29 10:56:28
问题 My application dynamically builds and runs complex queries to generate reports. In some instances I need to get multiple, somewhat arbitrary date ranges, with all other parameters the same. So my code builds the query with a bunch of joins, wheres, sorts, limits etc and then runs the query. What I then want to do is jump into the Builder object and change the where clauses which define the date range to be queried. So far, I have made it so that the date range is setup before any other wheres

how to make laravel query builder like codeigniter active record

£可爱£侵袭症+ 提交于 2019-11-29 08:53:11
I have a problem with Laravel 4 query builder, i want make a re-usable method public function getData($where=array()) { // $where = array('city' => 'jakarta', 'age' => '25'); return User::where($where)->get(); // this will produce an error, because i think laravel didn't support it } In CodeIgniter it easy to passing array to active record : public function getData($where=array()) { $rs = $this->db->where($where)->from('user')->get(); return $rs->result(); } // it will produce : // SELECT * FROM user WHERE city = 'jakarta' AND age = '25' Any idea how to have it on Laravel 4 query builder? I

Laravel query builder General error 2031

微笑、不失礼 提交于 2019-11-29 07:36:22
Below is my query using Laravel query builder: $begin = new DateTime('2016-07-01'); $end = new DateTime('2016-07-31'); $startDate = $begin->format('Y-m-d 00:00:00'); $endDate = $end->format('Y-m-d 23:59:59'); $deposit = $depositModel->select(DB::raw('user_deposit.user_id as user_id, sum(user_deposit.amount) as total_deposit, null as total_withdraw')) ->whereBetween('date_time', [$startDate, $endDate]) ->where('user_deposit.status', 1) ->groupBy('user_deposit.user_id'); $withdraw = $withdrawModel->select(DB::raw('user_withdraw.user_id as user_id, null as total_deposit, sum(user_withdraw.amount)

Using JOIN in Symfony2/Doctrine SQL

ε祈祈猫儿з 提交于 2019-11-29 06:52:31
问题 I have a problem while trying to USE QueryBuilder OR DQL. I have the following relation: User <-1:n-> Profile <-n:m-> RouteGroup <-1:n-> Route I would like to make a DQL that lists all the routes that a specific user has access. I can get this information with the following code: $usr = $this->container->get('security.context')->getToken()->getUser(); foreach ($usr->getProfiles() as $profile){ foreach ($profile->getRoutegroups() as $routegroup){ var_dump($routegroup->getRoutes()->toArray());

Symfony2 QueryBuilder join ON and WITH difference

℡╲_俬逩灬. 提交于 2019-11-29 05:25:51
I'm new with Symfony2 and I built successfully my first join through QueryBuilder and Doctrine 2. Probably this is a stupid question but both on-line and in the Symfony2's methods I was unable to find anything for understanding the difference between the join clauses "WITH" and "ON". For example this is my join code: ->leftJoin('EcommerceProductBundle:ProductData', 'pdata', 'WITH', 'prod.id = IDENTITY(pdata.product)') It works good but if I put ON instead of WITH I get the following error: [Syntax Error] line 0, col 200: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON' Why? I've seen