query-builder

laravel search multiple words separated by space

大城市里の小女人 提交于 2019-11-29 03:42:07
问题 I am new to laravel query builder, I want to search multiple words entered in an input field for example if I type "jhon doe" I want to get any column that contains jhon or doe I have seen/tried solutions using php MySQL but can't able to adapt to query builder //1. exploding the space between the keywords //2. using foreach apend the query together $query = "select * from users where"; $keywordRaw = "jhon doe"; $keywords = explode(' ', $keywordRaw ); foreach ($keywords as $keyword){ $query.=

Doctrine - self-referencing entity - disable fetching of children

懵懂的女人 提交于 2019-11-29 01:01:40
问题 I have a very simple entity(WpmMenu) that holds menu items connected to one another in a self-referencing relationship (adjecent list it's called)? so in my entity I have: protected $id protected $parent_id protected $level protected $name with all the getters/setters the relationships are: /** * @ORM\OneToMany(targetEntity="WpmMenu", mappedBy="parent") */ protected $children; /** * @ORM\ManyToOne(targetEntity="WpmMenu", inversedBy="children", fetch="LAZY") * @ORM\JoinColumn(name="parent_id",

Laravel Query Builder where max id

末鹿安然 提交于 2019-11-29 00:58:13
How do I accomplish this in Laravel 4.1 Query Builder? select * from orders where id = (select max(`id`) from orders) I tried this, working but can't get the eloquent feature. DB::select(DB::raw('select * from orders where id = (select max(`id`) from orders)')); Any idea to make it better? Tim Groeneveld You should be able to perform a select on the orders table, using a raw WHERE to find the max( id ) in a subquery, like this: \DB::table('orders')->where('id', \DB::raw("(select max(`id`) from orders)"))->get(); If you want to use Eloquent (for example, so you can convert your response to an

How can I select specific Columns with createQueryBuilder in ORM Symfony2?

隐身守侯 提交于 2019-11-28 22:43:08
I'm using createQueryBuilder to construct queries in Symfony2. But, I don't want to take all columns in this entity. How can I select only the ID and Name? $query = $this->getEntityManager()->createQueryBuilder(); $query ->select('d') ->from('AcmeBundle:Demo', 'd') ->leftjoin('d.otherEntity', 'o'); $query->setMaxResults(10); $results = $query->getQuery()->getResult(); Thank you so much, Try following, $fields = array('d.id', 'd.name', 'o.id'); //$fields = 'partial d.{id, name}, partial o.{id}'; //if you want to get entity object $query = $this->getEntityManager()->createQueryBuilder(); $query

Laravel 5.2 - pluck() method returns array

人走茶凉 提交于 2019-11-28 20:02:10
I'm trying to upgrade my project L5.1 -> L5.2. In upgrade guide there's one thing which isn't clear for me: The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck . The method signature remains the same. That's ok, rename refactoting from lists() to pluck() isn't a problem. But what with useful pluck() method which was in L5.0 and L5.1? From the 5.0 documentation : Retrieving A Single Column From A Row $name = DB::table('users')->where('name', 'John')->pluck('name'); What is the alternative for old pluck() method in L5.2? UPDATE: Example:

Symfony form query_buider and entity repository

笑着哭i 提交于 2019-11-28 17:47:48
问题 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

Laravel: how to use derived tables / subqueries in the laravel query builder

大城市里の小女人 提交于 2019-11-28 08:33:44
Edit: Though this question originally was specific for the query I'm describing underneath, the answer I got applies to almost all questions related to using derived tables / subqueries in Laravel Original Question: Lately I'm a bit stuck on the laravel query builder. It has some really nice features but I feel like it just isn't build for more complex database operations. This is the query I'm trying to build: select 'IFNULL(counted.product_count, 0) AS product_count', 'uncounted.value', 'uncounted.attribute_id', 'uncounted.attribute_option_id' from ( select 'counted.id', 'counted.attribute

Symfony2 Doctrine querybuilder where IN

橙三吉。 提交于 2019-11-28 07:33:32
I losted trilion hours google this but none of the solutions were good. I have this querybuilder: $qb2=$this->createQueryBuilder('s') ->addSelect('u') ->innerJoin('s.user','u') ->where("u.id IN(:followeeIds)") ->andWhere('s.admin_status = false') ->setParameter('user', $user) ->setParameter('followeeIds', $arrayFolloweeIds) ->orderBy('s.id','DESC') ->setMaxResults(15) ; I could do a second query and then do like $qb->getDQL() but have would i cache the query ? Error: Invalid parameter number: number of bound variables does not match number of tokens Ghassan Idriss You are setting the user

How to order by count in Doctrine 2?

元气小坏坏 提交于 2019-11-28 06:58:20
I'm trying to group my entity by a field (year) and do a count of it. Code: public function countYear() { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('b.year, COUNT(b.id)') ->from('\My\Entity\Album', 'b') ->where('b.year IS NOT NULL') ->addOrderBy('sclr1', 'DESC') ->addGroupBy('b.year'); $query = $qb->getQuery(); die($query->getSQL()); $result = $query->execute(); //die(print_r($result)); return $result; } I can't seem to say COUNT(b.id) AS count as it gives an error, and I do not know what to use as the addOrderby(???, 'DESC') value? what is the error you get when using

Laravel complains about query with duplicate named parameters

两盒软妹~` 提交于 2019-11-28 06:43:27
问题 When I do (in laravel): <?php \DB::select('SELECT * FROM my_table WHERE id = :id || id = :id', [ 'id' => 1, ]); It says: SQLSTATE[HY093]: Invalid parameter number (SQL: SELECT * FROM my_table WHERE id = :id || id = :id) But when I do (in pure php): <?php $dbh = new PDO('mysql:dbname=...', '...', '...'); $stmt = $dbh->prepare('SELECT * FROM my_table WHERE id = :id || id = :id'); $r = $stmt->execute([ 'id' => 1, ]); while ($row = $stmt->fetch()) { var_dump($row['id']); } It succeeds. What am I