query-builder

How to build query with selecting by value of foreign object's field

老子叫甜甜 提交于 2019-12-04 03:31:37
问题 What is the best way for querying by using the value of foreign object's field? Suppose I have these three classes. UnitResult class which describes amount of Units: @DatabaseTable public class UnitResult { public static final String ID_FIELD_NAME = "id"; public static final String UNIT_COLUMN_NAME = "unit"; public static final String RESULT_COLUMN_NAME = "result"; @DatabaseField(generatedId = true, columnName = ID_FIELD_NAME) public Integer id; @DatabaseField(foreign = true, canBeNull =

How do I get a “select count(*) group by” using laravel eloquent

梦想的初衷 提交于 2019-12-04 01:12:58
I would like to execute the follow sentence using laravel eloquent SELECT *, count(*) FROM reserves group by day The only solution occurs to me is to create a view in the DB, but I am pretty sure there is a way to do it in laravel way. You could use this: $reserves = DB::table('reserves')->selectRaw('*, count(*)')->groupBy('day'); As you wish to do it with Laravel Eloquent I assume you have a model name Reserve . In this case you can use this $reserve = Reserve::all()->groupBy('day')->count(); Khine Thu You could use: #Laravel Raw Expressions $reserves = DB::table('reserves') ->select(DB::raw(

How to use the ORMLite query builder to get the total records in a table

自作多情 提交于 2019-12-03 22:20:19
Similar to select count(*) from tablename; what should be query in ORMLITE i tried something like int total = dao.queryBuilder().("select count(*)"); How to use the ORMLite query builder to get the total records in a table ORMLite has a Dao.countOf() method that returns the total number of rows in a table: long numRows = dao.countOf(); You can also count the number of rows in a custom query by calling the countOf() method on the Where or QueryBuilder object. // count the number of lines in this custom query long numRows = dao.queryBuilder().where().eq("name", "Joe Smith").countOf(); PoeHaH for

How to get partial result from doctrine query builder

主宰稳场 提交于 2019-12-03 21:32:25
问题 I have a product entity in which it has an array as attributes: /** * @ORM\OneToMany(targetEntity="Shopious\MainBundle\Entity\ProductPicture", mappedBy="product", cascade={"persist","remove"}) */ protected $pictures; /** * @Accessor(getter="getCover") */ private $cover; public function getCover() { if($this->pictures->count() > 0) { return $this->pictures[0]; } return new ProductPicture(); } Now in my query builder, I have the following code: $query = $em->createQueryBuilder()->select('p') -

How to select fields of a contained association?

偶尔善良 提交于 2019-12-03 18:07:49
问题 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' ]) -

From Doctrine Query to QueryBuilder in a Simfony2 entity field type

独自空忆成欢 提交于 2019-12-03 09:22:20
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 all these queries in the entity field type. Moreover, rewriting all the queries for the use with a

How do I add a 'where not' to a QueryBuilder Query

泪湿孤枕 提交于 2019-12-03 09:04:35
I want to search the entire content tree but not specific tress that have a 'Do Not Search' property at their base. The Query Builder API page does not reference anything besides AND and OR. Is it possible to exclude paths from the search or can I only explicitly include paths? The first three lines are "/content AND /content/path/es". I want "/content AND NOT(/content/path/es)" map.put("group.1_path", "/content"); map.put("group.2_path", "/content/path/es"); map.put("group.p.or","false"); I have tried the next two both true and false and they have no effect. map.put("group.2_path.p.not",

Invalid PathExpression. Must be a StateFieldPathExpression

若如初见. 提交于 2019-12-03 00:04:55
问题 I get an error which is [Semantical Error] line 0, col 57 near 'room FROM AppBundle:bookings': Error: Invalid PathExpression. Must be a StateFieldPathExpression. I have two entities in AppBundle which are Room and Bookings. once I execute the query I get the error mentionned before. Here my query : $query = $em->createQuery( 'SELECT r ' . 'FROM AppBundle:Room r ' . 'WHERE r NOT IN ( ' . 'SELECT b.room ' . 'FROM AppBundle:Bookings b ' . 'WHERE NOT ( ' . 'b.check_out < :check_in ' . 'OR ' . 'b

DB query builder toArray() laravel 4

自古美人都是妖i 提交于 2019-12-02 21:43:56
I'm trying to convert a query to an array with the method toArray() but it doesn't work for the query builder. Any ideas for convert it? Example DB::table('user')->where('name',=,'Jhon')->get()->toArray(); toArray is a model method of Eloquent, so you need to a Eloquent model, try this: User::where('name', '=', 'Jhon')->get()->toArray(); http://laravel.com/docs/eloquent#collections If you prefer to use Query Builder instead of Eloquent here is the solutions $result = DB::table('user')->where('name',=,'Jhon')->get(); First Solution $array = (array) $result; Second Solution $array = get_object

Laravel 4: multiple where with or in eloquent

[亡魂溺海] 提交于 2019-12-02 15:31:21
问题 how to write this condition using laravel eloquent SQL select count(id) from fight where status = 'finished' and (user1 = 1 or user2 = 1) Laravel (not finished): Fight::where('user1','=',$uid)->orWhere('user2', '=', $uid)->count('id'); thanks, 回答1: This should do the work: Fight::whereStatus('finished')->where(function($q) use ($uid) { $q->where('user1',$uid)->orWhere('user2', $uid); })->count('id'); EDIT Answering comment: Fight::whereIn('status', ['finished', 'cancelled'])->where(function(