Get values from a doctrine collection with composite key - doctrine

4 for on on my applications with Doctrine.
In there I'm using the following doctrine command to retrieve person object collection
//query
$people = $q->execute();
This return 20 objects. The primary key of the person object is a composite key with three attributes. Those are
id
department_id
name
I need to get person objects by searching in it as follows.
$id = 10;
$department_id = 1;
$name = "abc";
$people->get($id, $department_id, $name);
But this doesn't work and not give correct results. I tried with this and it gives null results which seems my collections primary key is not set.
$people->getKeyColumn();
I don't want to go through a foreach loop in collection and process it because when I deal with about 500 people, it slow down my application.
Can some one help me with this issue to get values from a doctrine collection.

Can you use something like this?
$people = Doctrine::getTable('Persons')
->createQuery()
->where('id = ? AND department_id = ? AND name = ?', array($id, $department_id, $name))
->execute();
It will get you a DoctrineCollection already filtered by the parameters provided.
'Persons' here is a Doctrine model name, not a table name from mySQL.

You can also use Doctrine's magic finders findBy*():
$people = Doctrine_Core::getTable('Persons')
->findByIdAndDepartmentIdAndName($id, $department_id, $name);

Related

Sorting a field based on several values in Laravel

I have an array of city codes
$cities=[9,12,14,2,3,4,5,6,8,10,11,13]
My posts table has a foreign key named city_id
I want sorting posts based on the values of this array
In this way: the first posts of the city 9 then the posts of the city 12 and then posts of city 14 and etc to be loaded
I tried using this method but this is wrong
$posts->orderByRaw('city_id in ? desc',$cities);
Can you help me find the best solution?
The only way i can i think of doing something like that(at least right now) is by doing something like so
$all_posts = [];
$cities=[9,12,14,2,3,4,5,6,8,10,11,13];
foreach ($cities as city) {
$city_posts = Post::whereRaw('city_id = ?', $city)->orderByRaw('created_at DESC');
array_push($all_posts, $city_posts);
}
dd($all_posts);
1st find all the posts relevant to cities and then sort w.r.t given order like
$cities = [9,12,14,2,3,4,5,6,8,10,11,13]; // order you'd like to see with posts
$posts = Post::whereIn('city_id', $cities)->sort(function($a, $b) uses ($cities) {
$pos_a = array_search($a->getAttributes()['city_id'], $cities);
$pos_b = array_search($b->getAttributes()['city_id'], $cities);
return $pos_a - $pos_b;
})->get();
// $posts contains the required order with city_ids
You can use raw query with "CASE something THEN index" this way you tell the query to see something as index so you can assign 0 to the first item in your array.
$sql .= "....";
foreach($cities as $index => $city) {
$sql .= "CASE {$city} THEN {$index}";
}
$sql .= "....";
Thanks to the friends solution,I used this method, and I think it's less complicated than the suggested methods of friends
$posts=$posts->orderByRaw('FIELD(city_id, '.$cities->implode( ', ').') asc')->orderBy('created_at','desc')->get();

Laravel Model::find() auto sort the results by id, how to stop this?

$projects = Project::find(collect(request()->get('projects'))->pluck('id')); // collect(...)->pluck('id') is [2, 1]
$projects->pluck('id'); // [1, 2]
I want the result to be in the original order. How do I achieve this?
Try $projects->order_by("updated_at")->pluck("id"); or "created_at" if that's the column you need them ordered by.
Referencing MySQL order by field in Eloquent and MySQL - SELECT ... WHERE id IN (..) - correct order You can pretty much get the result and have it order using the following:
$projects_ids = request()->get('projects'); //assuming this is an array
$projects = Project::orderByRaw("FIELD(id, ".implode(',', projects_ids).")")
->find(projects_ids)
->pluck('id'));
#Jonas raised my awareness to a potential sql injection vulnerability, so I suggest an alternative:
$projects_ids = request()->get('projects');
$items = collect($projects_ids);
$fields = $items->map(function ($ids){
return '?';
})->implode(',');
$projects = Project::orderbyRaw("FIELD (id, ".$fields.")", $items->prepend('id'))
->find($projects_ids);
The explanation to the above is this:
Create a comma separated placeholder '?', for the number of items in the array to serve as named binding (including the column 'id').
I solve this by querying the data one by one instead mass query.
$ids = collect(request()->get('projects'))->pluck('id');
foreach($ids as $id){
$projects[] = Project::find($id);
}
$projects = collect($projects);
$projects->pluck('id');
I have to do this manually because laravel collection maps all the element sorted by using ids.

Only one row is returned using where in with comma seperated value in Laravel raw query

I am developing a php project using Laravel 5.2. In my app I am retrieving records from database using manual query. But I am having a problem with retrieving records by using where in statement with csv.
Example how I am retrieving
$csv = "1,3,5";
$sql = "SELECT * FROM `items` WHERE `id` IN (?)";
$rows = DB::select($sql,[$csv]);
As you can see above I am retrieving three rows. But it returns only one row where id is 1. Why is that?
You can't do it like that. Each entry in your csv is a separate parameter, so for your code you would actually need IN (?, ?, ?), and then pass in the array of values. It would be pretty easy to write the code to do this (explode the string to an array, create another array of question marks the same size, put it all together).
However, you are using Laravel, so it would be easier to use the functionality Laravel provides to you.
Using the query builder, you can do this like:
$csv = "1,3,5";
// turn your csv into an array
$ids = explode(",", $csv);
// get the data
$rows = DB::table('items')->whereIn('id', $ids)->get();
// $rows will be an array of stdClass objects containing your results
dd($rows);
Or, if you have an Item model setup for your items table, you could do:
$items = Item::whereIn('id', $params)->get();
// $items will be a Collection of Item objects
dd($items);
Or, assuming id is the primary key of your items table:
// find can take a single id, or an array of ids
$items = Item::find($params);
// $items will be a Collection of Item objects
dd($items);
Edit
If you really want to do it the manual way, you could use a loop, but you don't need to. PHP provides some pretty convenient array methods.
$csv = "1,3,5";
// turn your csv into an array
$ids = explode(",", $csv);
// generate the number of parameters you need
$markers = array_fill(0, count($ids), '?');
// write your sql
$sql = "SELECT * FROM `items` WHERE `id` IN (".implode(',', $markers).")";
// get your data
$rows = DB::select($sql, $ids);

doctrine 2 findby function , get keys and values

can i get the keys and values dynamically.
in php, you would: foreach ($array as $key => $value)
but you can do this for :
$this->_em->getRepository('Members')->findBy(array('id' =>5));
any way to get the keys from this with their values..?
i can do this by turning it into an array and extract it but i wouldnt get any association results inside the array ..
i want to do this as i want to be able to extract all properties and values of this object and extract all other objects within it too..
Ih had the same issue as you now have and after some research i just found a solution which you might be interested.what you need is an associative array of keys/values and not an object.findBy()method only returns entity OBJECT.so you will need to use DQL(doctrine query language).
//create a QueryBuilder instance
$qb = $this->_em->createQueryBuilder();
$qb->add('select', 'a')
//enter the table you want to query
->add('from', 'Members a')
->add('where', 'a.id = :id')
//order by username if you like
//->add('orderBy', 'a.username ASC')
//find a row with id=5
->setParameter('id', '5');
query = $qb->getQuery();
//if you dont put 3 or Query::HYDRATE_ARRAY inside getResult() an object is returned and if you put 3 an array is returned
$accounts = $query->getResult(3);
from doctrine documentation:
13.7.4. Hydration Modes
Each of the Hydration Modes makes assumptions about how the result is
returned to user land. You should know about all the details to make
best use of the different result formats:
The constants for the different hydration modes are:
Query::HYDRATE_OBJECT
Query::HYDRATE_ARRAY
Query::HYDRATE_SCALAR
Query::HYDRATE_SINGLE_SCALAR
To learn more about 'The Query Builder' please refer to doctrine2 documentation
Update:
To fetch associated Entities you will need to define fetch joins.Here is an example provided in doctrine documentation:
$dql = "SELECT b, e, r, p FROM Bug b JOIN b.engineer e ".
"JOIN b.reporter r JOIN b.products p ORDER BY b.created DESC";
$query = $entityManager->createQuery($dql);
$bugs = $query->getArrayResult();
foreach ($bugs AS $bug) {
echo $bug['description'] . " - " . $bug['created']->format('d.m.Y')."\n";
echo " Reported by: ".$bug['reporter']['name']."\n";
echo " Assigned to: ".$bug['engineer']['name']."\n";
foreach($bug['products'] AS $product) {
echo " Platform: ".$product['name']."\n";}
echo "\n";}
The code mentioned above will fetch your entities as array of arrays and you can do whatever you want with $keys and $values.
Hope this helps...

Sort field in relations table (one to many), how to insert the sort number?

I have two tables, content and images (and a ContentImages table for the one to many relation, so that's actually 3 tables).
The following code saves the relation (in the action > updateContentFromRequest() ):
$ids = $this->getRequestParameter('contentImages');
if( isset($ids) ){
$ImagesTable = Doctrine::getTable('Content')->getRelation('Images')->getTable();
$associationName = Doctrine::getTable('Content')->getRelation('Images')->getAssociationTable()->getOption('name');
$this->content->$associationName->delete();
foreach ($ids as $id){
$id = explode('/', $id);
$this->content->get('Images')->add($ImagesTable->find($id));
}}
I changed the model to include a sort field in the ContentImages table:
content_id
image_id
sort (numeric)
The sort number is simply that, a number (0,1,2,3 etc)
$sort = $this->getRequestParameter('contentImagesSort');
How do I save the sort number? I do not want to add a sort field to the Image table because that could create difficulties when images are re-used across more content items. When a content item is new I do not know the ID yet so I'm a bit stumped...
If you have generated models you can add to your setUp method the orderBy parameter:
$this->hasMany('PICTURE as PICTURES', array(
'local' => 'BRAND_ID',
'foreign' => 'PICTURE_ID',
'refClass' => 'BRAND_PICTURE',
'orderBy' => 'your_field DESC'
));
orderBy should do the trick
You should add One to Many associations on your join Table, like:
ContentImages:
relations:
Image:
local: image_id
foreign: id
Content:
local: content_id
foreign: id
As such, you will be able to query directly the join table like this :
$q = Doctrine_Query::create()
->from('Content c')
->leftJoin('c.ContentImages ci')
->leftJoin('c.Images i')
->execute();
Then you can access to ContentImages using
$content->ContentImages->setSort('your sort');
This should do the trick :)
I do things a little a differently from what you've got above so not completely sure this is what you're asking, but can't you just save the object with whatever is needed for it?
$association = // load up existing entry from ContentImages using whatever method
$association->setSort($the_sort_I_want_to_add);
$association->save();
Or querying it...
$association = // load up existing entry from ContentImages using whatever method
$my_unknown_sort = $association->getSort();
Hope that helps.

Resources