User Table -
column name - location_id
values = 4,5
Location table
column name - id , location_name
values = 4 nagpur
5, Akola
i want to display location names in listbox by joining location_id of user table,
Suppose in location_id column of user table 4,5 values are present then in my select box this only displays nagpur, Akola from location table.
i cannot understand how to explode values of location_id table before select and display only those values which are available in location_id column from location table.
public function getUserLocations() {
$this->loadModel('User');
$getlocations = $this->User->find('list', array(
'fields' => array('Location.id','Location.location_name'),
'joins' => array(
array(
'table' => 'locations',
'alias' => 'Location',
'type' => 'LEFT',
'conditions' => array('FIND_IN_SET(User.location_id,Location.id)')
)
),
));
$this->set('getlocations', $getlocations);
}
below is my code ..
Explode the variable like this :
$getlocations = $this->User->find('list', array(
'fields' => array('Location.id'));
$location_ids[] = explode (',', $getlocations);
Now you will get the data in array format. Modify it according to your needs
Related
So I have 2 tables.
Table 1: (person_identity)
Id_person(PK), name, address, telp
Table 2: (Student)
Id_person(FK), no_student (PK), status, parent
How to input data to name, address, no_student, parent and status at the same time with the same Id_person but at 2 different table?
You can do like this
$dataPersonIdentity = ['name' => 'Abc', 'address' => 'This is address', 'telp' => 'this is telp'];
$this->db->insert('person_identity',$dataPersonIdentity);
$personIdentityId = $this->db->insert_id();
$dataStudent = ['Id_person' => $personIdentityId, 'no_student' => 8, 'status' => 1, 'parent' => 1];
do insert in the first table (person_identity)
and then do insert in the second table (student)
I have a Venue table with 20+ columns.
I also have a favorite_venues table with columns
id | userId | venueId
Here is my code to get venues
$this->Venue->virtualFields = array(
'bookedCount' => "SELECT count(*) FROM bookings WHERE venueId = Venue.id"
);
$result = $this->Venue->find('all', array('conditions'=>$conditions,
'order' => array('Venue.bookedCount DESC'),
'limit' => 20,
'offset' => $offset * 20
));
i want to add a condition if i send userId, it should check every venue if its added to favourite list or not and set
$venue['isFavorite'] = yes/no
I dont want a for loop and check every venue i get. is there any way i can incorporate in same Mysql query in cakephp. I am not sure how to put yes/no as virtual field
You can LEFT join in your favorite_venues table, and then for example use a CASE statement in a virtual field that checks whether there is a linked row.
Here's an untested example to illustrate what I mean. I don't know how the user ID is involved, so you got to figure that on your own.
$this->Venue->virtualFields = array(
'bookedCount' => "SELECT count(*) FROM bookings WHERE venueId = Venue.id",
'isFavorite' => 'CASE WHEN FavoriteVenues.id IS NOT NULL THEN "yes" ELSE "no" END'
);
$result = $this->Venue->find('all', array(
'conditions' => $conditions,
'order' => array('Venue.bookedCount DESC'),
'limit' => 20,
'offset' => $offset * 20,
'joins' => array(
array(
'table' => 'favorite_venues',
'alias' => 'FavoriteVenues',
'type' => 'LEFT',
'conditions' => array(
'FavoriteVenues.venueId = Venue.id',
)
)
),
// don't forget to group to prevent duplicate results
'group' => 'Venue.id'
));
See also
Cookbook > Models > Virtual Fields > Virtual fields set in controller with JOINS
Cookbook > Models > Associations: Linking Models Together > Joining Tables
MySQL 5.7 Reference Manual / SQL Statement Syntax / ... / CASE Syntax
I'm trying to translate an SQL query into a CakePHP query. Where the SQL query returns one row, the CakePHP query returns 2 rows with the columns id & cover in the first row & files_with_cover in the second.
Naturally I'd like one row with all columns.
SQL query:
SELECT s1.id, s1.cover, COUNT(s2.id) files_with_cover FROM songs s1 LEFT JOIN songs s2 ON s2.cover=s1.cover WHERE s1.source_path=$path
CakePHP query:
$result = $this->Song->find('first', array(
'joins' => array(
array(
'table' => 'songs',
'alias' => 'Song2',
'type' => 'LEFT',
'conditions' => array('Song2.cover = Song.cover')
)
),
'fields' => array('Song.id', 'Song.cover', 'count(Song2.id) as files_with_cover'),
'conditions' => array('Song.source_path' => $file)
));
EDIT, example data set:
id cover source_path
-----------------------
1 image1 /example/1
2 image1 /example/2
3 image1 /example/3
4 image2 /example/4
When doing a select with the conditional source_path "/example/1" I want the following result:
id = 1, cover = image1, files_with_cover = 3.
The printout from the $result array may give a clue. Not sure why the files_with_cover column ends up in a separate row.
Array
(
[Song] => Array
(
[id] => 1
[cover] => cover1.jpeg
)
[0] => Array
(
[files_with_cover] => 3
)
)
Using CakePHP 2.8.1 & MySQL
What you receive there isn't two (database) rows, just a not entirely grouped single row. You need to use virtual fields if you want everything grouped under the main model alias.
$this->Song->virtualFields['files_with_cover'] = 'count(Song2.id)';
'fields' => array('Song.id', 'Song.cover', 'files_with_cover'),
See also Cookbook > Models > Virtual Fields
So, I want to have table with users name, grades and subjects. Table will display only his grades. So I'm generating subject in foreach loop and reading grades depending on his id.
For subject I want to have an array which will contain infos about subject (teacher, classroom, etc.)
For now I have this array:
$subjects = array();
$getSubjects = mysqli_query($con, "SELECT * FROM predmeti");
while ($subject = mysqli_fetch_array($getSubjects)) {
$subjects[]= array(
$subject['subject_name'] => array(
'id' => $subject['id'],
'name' => $subject['name'],
'teacher' => $subject['teacher'],
'short_name' => $subject['short_name'],
'classroom' => $subject['classroom']
)
);
I know this isn't right. I can't get data for each subject.
Could you please help me?
You're accessing the columns by name while mysqli_fetch_array() returns only an integer indexed array. Have you tried mysqli_fetch_assoc()?
I am working with CakePHP 1.3 version for search functionality using Search Plugin.
I have three models:
Demo,
Country
State
Demo has two foreign keys, country_id and state_id. State has the foreign key country_id.
What I am doing is, I have search form which have country & state drop down which fetch all data from countries & states table. When i search any of country from dropdown & submit the form it will show me below error. If i search using only state dropdown i get the correct result.
When I execute the search query, I get the error
'Column 'country_id' in where clause is ambiguous'
My query is:
SELECT `Demo`.`id`, `Demo`.`demo2`, `Demo`.`desc`, `Demo`.`subject`, `Demo`.`gender`, `Demo`.`country_id`, `Demo`.`state_id`, `Demo`.`image_url`, `Country`.`id`, `Country`.`name`, `State`.`id`, `State`.`country_id`, `State`.`description` FROM `demos` AS `Demo` LEFT JOIN `countries` AS `Country` ON (`Demo`.`country_id` = `Country`.`id`) LEFT JOIN `states` AS `State` ON (`Demo`.`state_id` = `State`.`id`) WHERE `country_id` = 2
Model relationships in Demo table:
var $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
The controller query to fetch all Country in dropdown is:
$country=$this->Country->find('list'); //just display the list of country in dropdown
The query search the data from all fields except Country (country_id), because it will not know which country_id it is looking for from table Demo or table State. I need the country_id from the demo table to get the correct result.
As I understand you want to make a find over Demo for a specific country_id.
Well you should define which "country_id" you're using because more than one of those tables
has such a column.
Just use Demo.country_id in the conditions array:
array('conditions' => array('Demo.country_id' => 2));
And you should see some SQL generated by Cake like this:
SELECT `Demo`.`id`, `Demo`.`demo2`, `Demo`.`desc`, `Demo`.`subject`, `Demo`.`gender`, `Demo`.`country_id`, `Demo`.`state_id`, `Demo`.`image_url`, `Country`.`id`, `Country`.`name`, `State`.`id`, `State`.`country_id`, `State`.`description` FROM `demos` AS `Demo` LEFT JOIN `countries` AS `Country` ON (`Demo`.`country_id` = `Country`.`id`) LEFT JOIN `states` AS `State` ON (`Demo`.`state_id` = `State`.`id`) WHERE `Demo`.`country_id` = 2
Try this:
SELECT
Demo.id,
Demo.demo2,
Demo.desc,
Demo.subject,
Demo.gender,
Demo.country_id,
Demo.state_id,
Demo.image_url,
Country.id,
Country.name,
State.id,
State.country_id,
State.description
FROM demos AS Demo
LEFT JOIN countries AS Country ON (Demo.country_id = Country.id)
LEFT JOIN states AS State ON (Demo.state_id = State.id) WHERE Demo.country_id = 2