I would like to insert in relation table an other attribute ("tiers_temps" in my example) that ids, is it possible with the function "link()" or others ?
I use actually this codes, but I must update the filed "tiers_temps" :
$Examen->link('Users', $listAdd);
with the table "user_has_examen" structure :
Field Type Null Key Default Extra
user_id int(11) NO PRI 0
examen_id int(11) NO PRI 0
tiers_temps int(11) YES NULL
There is un error in your proposition. The object $user refers to the table "user" and not "user_has_examen" (not descibe before, that's right) so "tiers_temps" doesn't exist for user.
This solution works but I loop access Doctrine...
foreach($user as $userId){
$UserExam = Doctrine::getTable('UserExamen')->findByDql('user_id = ? AND examen_id = ?', array($userId, $exam))->getFirst();
if($UserExam->tiers_temps != $users[$userId]){
$UserExam->tiers_temps = $users[$userId];
$UserExam->save();
}
}
I didn't find a nice way to interact with an intermediate item. But you can act on the relations to retrieve all user, and then, apply your tiers_temps value before saving your Examen:
$Examen->link('Users', $listAdd);
$users = $Examen->Users;
foreach ($users as $user)
{
$user->tiers_temps = '45';
}
When I find myself in these situations I prefer to give up the table many to many Identifying and move to a structure like this:
id
user_id
examen_id
tiers_temps
because I find it very inconvenient to manage the value tiers_temps in the structure you used.
Related
I would like to create a new record in a PostgreSQL database table with Laravel eloquent but the table id is not auto incremented and it's make my life more difficult.
Here is the sample structure:
CREATE TABLE billing (
id int8,
companyname varchar(255) NOT NULL,
...
PRIMARY KEY (id),
);
The id field isn't auto incremented and I can't change this. Furthermore I can't write triggers.
In Laravel I would like to do something like this:
$billing = new Billing;
$billing->id = DB::raw("nextval('sequence')");
$billing->companyname = $request->companyname;
// ...
$billing->save();
It's working but it seems messy.
How could I simplify the DB (id) part of my code?
Thanks!
Try with
$billing->id = DB::getPdo()->lastInsertId(); // + 1 ?
with this
$nextval=DB::select(DB::raw("SELECT nextval('".(new Billing())->getTable()."_id_seq') as seq"))[0]->seq;
$billing = new Billing;
$billing->id = $nextval;
$billing->companyname = $request->companyname;
// ...
$billing->save();
Trying to make a loop for EditableGrid code.
This is how it looks now.
$grid->addColumn('id', 'ID', 'integer');
$grid->addColumn('site', 'Site', 'string');
So if I need to add a new column to the page, I add a new column in MySQL database and also add a new row in this code, like:
$grid->addColumn('newcolumn', 'A brand new column', 'string');
In order to automatically add new columns to the page I want to make a loop, which gets inputs for the first argument (name of the field in the database) taken from the table:
CREATE TABLE price (
id INT(11) NOT NULL AUTO_INCREMENT,
site VARCHAR(50) NOT NULL,
and the other two arguments (label that will be displayed in the header and data type of the column in MySQL) taken from this table:
CREATE TABLE header (
header_name VARCHAR(50) NOT NULL,
header_type VARCHAR(50) NOT NULL,
Ok, think I found the solution. In order to create the loop, we create 2 queries, which are:
$get=$mysqli->query('SELECT header_name, header_type FROM header');
$get1=$mysqli->query('SHOW COLUMNS FROM price');
then we make a loop
while($row = mysqli_fetch_assoc($get) and $row1 = mysqli_fetch_assoc($get1)){
$grid->addColumn(''.$row1['Field'].'', ''.$row['header_name'].'', ''.$row['header_type'].'');}
I, guess, that's it. Also, if you need to exclude some of the columns, use this piece of code:
if($row1 == 'id' || $row1 == 'site')
continue;
I need to display just the last date entry for a person in a table, taken from joined tables.
I only seem to able to display the first entry or all.
Controller
public function session_view($id)
{
$data['main_view'] = 'session_view';
$data['view'] = $this->Swim_model->CRUD_read_session($id);
$this->load->view('load_view',$data);
}
Model
public function CRUD_read_session($sessionid)
{
return $this->db->select('*')
->from('sessionsandswimmers')
->join('child', 'ID = SwimmersID')
->join('swimmersawards', 'PersonID = ID')
->join('awards', 'AwardID = AwardsID')
->order_by('LastName')
->order_by('DateAwarded')
->where('SessionID', $sessionid)
->get();
}
View
foreach ($view->result() as $row)
{
echo '<tr>'.$row->FirstName.'</td><td'.$row->LastName.'</td><td>'.$row->Description.'</td><td>'.$row->DateAwarded.'</td></tr>';
}
Result
As you can see, there are several entries for each person (except 1st).
I need to display just the latest date entry for each person.
So there should only be 4 entries.
Table structure
sessionsandswimmers - each session has 4 swimmers
1 tempid Primary int(11) No None AUTO_INCREMENT
2 SessionID int(11) No None
3 SwimmersID int(11) No None
4 SessionSeasonID int(11) No None
5 Year int(11) No None
6 LocationSS int(11) No None
child - gets the swimmers name
swimmersawards - multiple entries per child
1 PersonID int(11) No None
2 AwardsID int(11) No None
3 DateAwarded date No None
awards - gets the name of the award
change ->order_by('DateAwarded') to ->order_by('DateAwarded','DESC') . Need to specify which type of ordering you want. And for just one entry per person use :
->group_by('FirstName,LastName')
If I understood correctly, the result you displayed is the table structure. Where you want the latest entry by date for each "name". If this is right then follow,
SELECT DISTINCT `name` FROM `<your-joined-tables>` order by `date` DESC
Sorry I am not good with CI's query builder.
Hope it works.
i use kohana framework and i am trying to code recursive function to create category tree.
My Categories Table
id int(11) NO PRI NULL auto_increment
name varchar(50) NO NULL
parent_id int(11) NO NULL
projects_count int(11) NO NULL
My Example Which Is Not Work
public static function category_list($parent_id = 0)
{
$result = Database::instance()->query('
SELECT name, projects_count
FROM project_categories
WHERE parent_id = ?',
array($parent_id)
);
$project_categories = array();
foreach($result as $row)
{
$project_categories[] = $row;
Project_Categories_Model::factory()->category_list($parent_id + 1);
}
return $project_categories;
}
Using this kind of hierarchical data implementation is highly non-optimal, because to get every subcategory you need do a separate query to the database. Like here you want to create recursion function.
If you still can change your table architecture please check Managing Hierarchical Data in MySQL.
This article describes a solution, how to fetch the whole hierarchy in one time query, so the recursive function will not be necessary.
I want to integrate an async treeview in PHP. Now i want to know how to generate a source php file with PHP.
Now my database structure is like this:
Create table School(
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(50),
primary key(id)
);
Create table Class(
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(50),
school_id int(10),
primary key(id),
foreign key(school_id) reference School(id) on delete cascade
);
Student(
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(50),
class_id int(10),
primary key(id),
foreign key(class_id) reference Class(id) on delete cascade
);
School -> Class -> Student
Now, i want to realize the treeview. Do you have any ideas of implementing it?
Thanks a lot.
Additional question: When i finish the treeview. If i click the items in the treeview, it will generate a result table by clicking. Do you know how to do that?
However, firstly i should finish the treeview.
So basically you need to (pseudo code):
$tree = array();
/**
* the following line essentially executes the Query:
* SELECT * from schools;
* and returns all the rows in an array like
* $schools = Array(0=>array('id'=>1, 'name' => 'name'))
*/
$schools = $db->schools()->selectAll();
foreach($schools as $school)
{
$schoolArr = array('text' => $school['name']);
/**
* Similar to the calls to school above except the query would be:
* SELECT * from class where class.school_id = $school['id']
* $school['id'] is the pk from the particular school record
*/
$classes = $db->classes()->select("school_id = ?", $school['id']);
$classesArr = array();
foreach($classes as $class)
{
$classArr = array('text' => $class['name']);
/**
* Similar to the calls to school above except the query would be:
* SELECT * from student where student.class_id = $class['id']
* $class['id'] is the pk from the particular class record
*/
$students = $db->students()->select('class_id = ?', $class['id']);
$studentsArr = array();
foreach($students as $student)
{
$studentsArr[] = array('text' => $student['name']);
}
$classArr['children'] = $studentsArr;
$classesArr[] = $classArr;
}
$schoolArr['children'] = $classesArr;
$tree[] = $schoolArr;
}
$jsTreeString = json_encode($tree);
Now obviously as you go through each loop you need to be assinging your other tree properties. then when youre all done just json_encode the array and echo it where i needs to be. At least thats how id work it. Note though, that you can probably do this without an individual query using some joins but i didnt want to get into all that - youll definitely wann explore that though if performance is at all an issue.
If you want this treeview to be done with jQuery I would suggest using this plugin. You just need to run one JS function and it will work. And all your PHP part has to do is get data from db and generate a HTML that jQuery plugin can transform into treeview.
It looks like this article/example code might be of interest to you in implementing the tree view.