query-builder

From Doctrine Query to QueryBuilder in a Simfony2 entity field type

半城伤御伤魂 提交于 2020-01-01 03:31:11
问题 I'm using the entity Field Type in a Symfony2.1 form. Here, I'm going to use the query_builder param to return only entities that matches a long-complex query (see the example in the official docs). Obviously the query_builder param for the entity field type accepts a Doctrine QueryBuilder object. On the other side, I have large entity repositories with complex DQL queries obtained by the EntityManager 's createQuery() function which returns a Doctrine Query object. So, I cannot directly use

DB->count() returning different value from count(DB->get())

冷暖自知 提交于 2020-01-01 02:41:09
问题 I have the simplest of queries that I'm trying to run DB::table('user_visits')->groupBy('user_id')->count(); But it's returning the wrong number, 8. If I change it to this: count(DB::table('user_visits')->groupBy('user_id')->get()); Then it returns the correct number, 34. Why are these not the same value? Here's my table structure user_visits( user_id, date_visited, num_clicks ) 回答1: A note on debugging The queries generated by those two different approaches are completely different, and is

Missing insert() method on Doctrine DBAL Query Builder

做~自己de王妃 提交于 2019-12-31 00:57:06
问题 I feel like I'm having a moment where I'm missing something small here; I've been having issues using the insert() method on the QueryBuilder component on Dotrine DBAL 2.2.x / 2.3.x. I did some investigation and here's the snippet from the QueryBuilder page from the DBAL Documantation The \Doctrine\DBAL\Query\QueryBuilder supports building SELECT, INSERT, UPDATE and DELETE queries. Which sort of query you are building depends on the methods you are using. It goes on further to explain code

CakePHP 3 - Count many-to-many Association

家住魔仙堡 提交于 2019-12-25 16:39:53
问题 I have a database with Rounds and Users. Rounds belongsToMany Users and Users belongsToMany Rounds, so a many-to-many relation. A join table rounds_users was added to do this. EDIT: Used the incorrect phrase here. I meant 'belongsToMany' instead of 'hasMany' Now I want to retrieve a list of Rounds, together with the number of linked Users per round. In case of a one-to-many relation something like the following would work: $rounds = $this->Rounds->find() ->contain(['Users' => function ($q) {

Selecting entries whose `date_field < NOW()`

喜欢而已 提交于 2019-12-25 15:29:11
问题 When I try to run the following query, it returns nothing: Item::where(\DB::raw('date_field < NOW()'))->get() The reason for this is, that is null is appended to the generated MySQL query like this: SELECT * FROM items WHERE date_field < NOW() is null; Why does the is null part get appended to the above query? 回答1: This is a known issue in Laravel and has been reported on their GitHub page. Use whereRaw() instead and pass a string: Item::whereRaw('date_field < NOW()')->get() 回答2: No idea why

Codeigniter Query Builder having clause with IN statement

倖福魔咒の 提交于 2019-12-25 07:58:38
问题 I am using CodeIgniter query builder. I want to add the having clause in there. My code looks as follows: (i omitted the other parts of the query): $this->db->having($filterarray); And i build the filterarray beforehand like this: $filterarray = array(); if (!empty($filters['interests'])) { $interestids = $this->interests_model->getAllIdsByDescription($filters['interests']); $filterarray['interests IN'] = $interestids; } My function getAllIdsByDescription looks like this: function

Laravel - Eloquent Create or Update Model

北战南征 提交于 2019-12-25 07:13:45
问题 I am trying to create a view counter for product views, so I need to track how many times the product is opened. So, what I have done is create a new table- Schema::create('user_add_views', function (Blueprint $table) { $table->integer('user_id') ->unsigned() ->index(); $table->integer('add_id') ->unsigned() ->index(); $table->integer('total_view') ->integer() ->default(1); //Removed all keys for making things faster //Foreign Keys $table->foreign('user_id') ->references('id') ->on('users');

Twig error - varibale does not exist for Sonata Admin

一笑奈何 提交于 2019-12-25 02:53:53
问题 I am passing query results to my twig view but it can't find that varibale. It' Sonata Admin details view with multiple tabs. Tamplate is renderig and when I pass a string it renders but when I try to call query builder with "getTransactions" it throws: Variable "card" does not exist. Code: $card = $this->getCardTransactions(); $showMapper->tab('Card transactions') ->add('Data', 'date', array( 'template' => "@AdminTemplates/details.html.twig", 'card' => $card )) ->end() ->end() And simple as

Laravel 4/5, order by a foreign column

痞子三分冷 提交于 2019-12-25 01:43:32
问题 In Laravel 4/5 how can order a table results based in a field that are connected to this table by a relationship? My case: I have the users that only store the e-mail and password fields. But I have an another table called details that store the name , birthday , etc... How can I get the users table results ordering by the details.name ? P.S.: Since users is a central table that have many others relations and have many items, I can't just make a inverse search like Details::... 回答1: I would

Laravel/SQL: where column Equals NOT and NULL

橙三吉。 提交于 2019-12-25 01:30:02
问题 LARAVEL 5.4 (but probably it's a more general SQL question) Hello! I have a table with a structure: Suppose it's my model 'Table'. I want a query which: uses (receives) variables : $id of array ['id', 'string', integer] where string is '<' or '>' $status_not_bad = bool; (if true - include all rows where 'status' !== 'bad' AND 'status' IS NULL); for example, we are given: $id = [['id', '>', 0]]; $status_not_bad = true; Table::thisquery() ... ->get(); "get rows where status is not bad and id >