Query the join Table of HABTM related Models - php

Im working with CakePHP since very recently..
I've got a table 'advertisers' and a table 'users', aswell as a table 'advertisers_users', that only contains 'advertiser_id' and 'user_id'.
. User hasAndBelongsToMany Advertiser.
I need an array in which i can find all advertisers (names and id's) that habtm to the current user id in my User-Controller. how can i do that in cakephp?
like
$this->User->Advertiser->find('all', array('conditions' => '?'));
Thank you all in advance!

$this->User->find(user_id);
Give an array with (if $this->User->recursive is set to 1 )
Array
(
[User] => Array
(
[id] => 2745
[name] => Chocolate Frosted Sugar Bombs
)
[Advertiser] => Array
(
[0] => Array
(
[id] => 123
[name] => blabla
)
[1] => Array
(
[id] => 124
[name] => blabla
)
[2] => Array
(
[id] => 125
[name] => blabla
)
)
)

Related

How to get Table field names at top of result array

Here is my Sample table:
Here is My Query:
SELECT * FROM table
Result for above query is:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[country] => x
)
[1] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
I need to get the field names in the first element of the array.
Expected result array is:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
How can I modify the Query to get this result?
Thanks in advance...
Display issues should generally be dealt with in application code, not SQL queries. If you have your values in an array (called $results, say), you can use this code to add the desired entry:
array_unshift($results, array_combine(array_keys($results[0]), array_keys($results[0])));
print_r($results);
Output:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
Demo on 3v4l.org
SELECT 'id' id, 'name' name, 'country' country
UNION ALL
SELECT id, name, country FROM table
ORDER BY 'id' != id
One way to do it with array_keys, array_combine and array_unshift. If I were you I will get the SQL result as $array variable and do some processing on php side like below on query side.
<?php
$array = [['id'=>1,'name'=>'a','country'=>'x'],['id'=>1,'name'=>'b','country'=>'y']];
$keys = array_keys($array[0]);
$first = array_combine($keys,$keys);
array_unshift($array,$first);
print_r($array);
?>
WORKING DEMO: https://3v4l.org/IqZVk

Insert auto generated multidimensional array to database

I need to create a db function for multidimensional array. How deep the array currently dont know, bcoz they will come from xml file.
I have a sample array
Array
(
[employee] => Array
(
[0] => Array
(
[name] => Array
(
[lastname] => Kelly
[firstname] => Grace
)
[hiredate] => October 15, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Printer
[id] => 111
[price] => $111.00
)
[1] => Array
(
[product] => Laptop
[id] => 222
[price] => $989.00
)
)
)
)
[1] => Array
(
[name] => Array
(
[lastname] => Grant
[firstname] => Cary
)
[hiredate] => October 20, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Desktop
[id] => 333
[price] => $2995.00
)
[1] => Array
(
[product] => Scanner
[id] => 444
[price] => $200.00
)
)
)
)
[2] => Array
(
[name] => Array
(
[lastname] => Gable
[firstname] => Clark
)
[hiredate] => October 25, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Keyboard
[id] => 555
[price] => $129.00
)
[1] => Array
(
[product] => Mouse
[id] => 666
[price] => $25.00
)
)
)
)
)
)
I need to enter these type of array to db and then retrieve them in a good non programmer readable format
I created 2 table... 1st for array key with array level field and another for key=value
I tried this
function array_Dump($array, $d=1){
if (is_array($array)){
foreach($array as $key=>$val){
for ($i=0;$i<$d;$i++){
$level=$i;
}
if (is_array($val)){
if (is_int($key)){
array_Dump($val, $d+1);
}else{
$query = "insert into xml_array (level, input) VALUES ('$level','$key')";
insert_sql($query);
array_Dump($val, $d+1);
}
} else {
$query = "insert into xml_data (array_id,level_id, array_key,array_value) VALUES ('$insert_id','$level','$key','$val')";
insert_sql($query);
}
}
}
}
Create a table like this:
attributes(id, parent_id, properties)
where id will be the primary key, parent_id will be the id of the parent record and properties will be a small json field with the atomic properties. This way you support any depth the XML may throw towards your direction.
As about non-programmer representation. For instance you could use a table (you can solve that with divs as well) which will contain a row for each element in the top level array. Such a row would contain separate columns for each property. When a property is an array, then the given cell will be a table of its own, which will be handled similarly as the first table. It is advisable to make the inner tables collapsible, so if one wants to see the main levels only, the user will not have to scroll for a long while.

Array parsing using array value

i have below array,and i have amenities id = 50,i need to show amenities name like 'Express check-out' using amenities id = 50 from this array using php.
Array
(
[amenities] => Array
(
[0] => Array
(
[id] => 0
[name] => Cash machine
[key] => CASHMACHINE
)
[1] => Array
(
[id] => 42
[name] => Express check-in
[key] => EXPRESSCHECKINSERVICE
)
[2] => Array
(
[id] => 50
[name] => Express check-out
[key] => EXPRESSCHECKOUTSERVICE
)
[5] => Array
(
[id] => 3
[name] => Wi-Fi
[key] => WIFISERVICE
)
)
)
There are many ways that your problem can be solved. Easy way can be as follow
function getAmenities($array,$id){
foreach($array['amenities'] as $tmp_arr)
if($tmp_arr['id']==$id)
return $tmp_arr['name'];
}
echo getAmenities($array,50);
I have not checked result but should work fine. Please let me know if this works for you
How do u create this Array?
Can't u just use the id as the key when u create it, like:
$key = $array2['id'];
$array['amenities'][$key] = $array2;

Filter Pagination results on Contained column

I have two tables in my database Members and Memberitems. I created two models for it. one is, Member and another is Memberitem
Member model has hasMany relation to Memberitem, and Memberitem model has belongsTo relations with my Member model.
Memberitem entries has specific categorization based on color, like, Red, Pink, Green, etc.
Now I want to select all the members who has atleast one Pink color memberitem using pagination.
Currently I am using:
$this->paginate = array(
'limit' => 5,
'contain' => array(
'Memberitem' => array(
'conditions' => array('Memberitem.color' => 'Pink')
)
)
);
But its showing all the Members.
output is this:
Array
(
[0] => Array
(
[Member] => Array
(
[id] => 1
[first_name] => fh
[last_name] => g
)
[Memberitem] => Array
(
[0] => Array
(
[id] => 1
[name] => item2
[color]=> Pink
)
)
)
[1] => Array
(
[Member] => Array
(
[id] => 2
[first_name] => ad
[last_name] => vd
)
[Memberitem] => Array
(
)
)
[2] => Array
(
[Member] => Array
(
[id] => 3
[first_name] => ae
[last_name] => sdi
)
[Memberitem] => Array
(
[0] => Array
(
[id] => 3
[name] => item1
[color]=> Pink
)
)
)
)
Its showing this result. Member of empty memberitem is still there. I want only 1st and 3 record in result.
This is a classic :-) It takes a while to get it though.
The Cake manual has an excellent peace on this. You should read: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations
So if you want only Members with certain MemberItems, you should query on the MemberItems and contain the Member Model. You can actually do this from Member Model:
$this->Member->Memberitem->find('all', array(
'conditions' => 'Memberitem.color = "Pink"',
'contain' => 'Member',
));
or maybe a bit more catered to you app:
$this->paginate['Memberitem'] = array(
'contain' => array('Member'),
'conditions' => array(
'Memberitem.color' => 'Pink',
),
);
$items = $this->paginate('Memberitem');
Hope that helps!

How to get nested array from MySQL select with joins

I have the models User, Post, Comment and Tag.
User creates Post.
Posts can have multiple Comment and Tag.
Each model has it's own table, so there are tables 'posts', 'comments' and 'tags'. Comments have a foreign key called 'post_id', while Tags have a many_to_many relation table called 'post_tags', in which there are two fields: 'post_id' and 'tag_id'.
I want to get a nested array like below:
Which MySQL queries should I run?
I suppose I need to alter the result with PHP to get my nested array. How?
Thanks a lot for your help :-)
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => First article
[content] => aaa
[created] => 2008-05-18 00:00:00
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[post_id] => 1
[author] => Daniel
[email] => dan#example.com
[website] => http://example.com
[comment] => First comment
[created] => 2008-05-18 00:00:00
)
[1] => Array
(
[id] => 2
[post_id] => 1
[author] => Sam
[email] => sam#example.net
[website] => http://example.net
[comment] => Second comment
[created] => 2008-05-18 00:00:00
)
)
[Tag] => Array
(
[0] => Array
(
[id] => 1
[name] => Awesome
)
[1] => Array
(
[id] => 2
[name] => Baking
)
)
)
[1] => Array
(
[Post] => Array
(...
You better of doing 3 queries.
first fetch post (, and left join the user if you need it), and store them like:
$list[$row['post_id']]['Post'] = $row;
then fetch all post-comments and store them as
$list[$row['post_id']]['Comment'][$row['comment_id']] = $row;
then fetch all post-tags
$list[$row['post_id']]['Tags'][$row['tag_id']] = $row;
that far more effective than trying to use a single query,
as a single query going to end up sending the same data multiple times

Categories