Relationships on Cakephp 2.0 - cakephp

I have two models : Absence, Students;
Absence fetch name and absence number from Students, now i want to linking model together, but i have been struggling to figure out how to map those relationships,
i dont have groups table, cause works well before, but now cake php said error by not have linking model., i used this following line before and work fine!
$this->loadModel("Student");
$siswas=$this->Student->find('all');
$this->set('students', $siswas);
but now i have error detected, named Internal server 500, and i don't know what i have to do.,
before my application is fine, but now is something wrong happens, now i need to linking model together, i need help for it..
any help would appreciated..
EDIT :
"My Problems has been Solved, the problem is because database is not working well thank for the answers"

Table structure would be :
Students Table
id student_name other fields
absents table
id student_id other fields
student Model realationship
$hasMany = array('Absent');
Absent Model realationship
$belongsTo = array('Student');

Related

Saving data with table associations in Grails

I have two database tables 'Student' and 'Course'. I want a one-to-one relation where there is foreign key of 'Course' table in 'Student' table. The foreign key 'course_id' is appearing in the Student table but whenever I try to save the Student data (after filling form), the data does not get saved and there is no error as well. I am very new to grails. Please guide me how I can save the data correctly.
The table structure is as desired but the data is not getting saved to the database. There is no issue with other code parts because the data is getting saved when I remove the association between the two tables.
Student controller
package com.grails
class studentController {
def save(Student student){
student.save()
}
}
Student domain class
class Student {
Course course
String firstName
String lastName
}
Course domain class
class Course{
String course_name
String duration
static hasOne = [student: Student]
//static belongsTo = [student: Student]
}
Note that grails 4 introduced a bug with one-to-one relationships that gives inconsistent behavior based on the owning side of the relationship: https://github.com/grails/grails-core/issues/11606
I'm not sure if this is the same problem you may be seeing without more information on your error, or even if you're on this version of grails.

Drop-Down isn't populated with the 'name' field when table name contains "companies"

I have this very strange issue I cannot explain:
The table drivers has the belongsTo association to the table transport_companies connected by the field drivers.transport_company_id. Usually in the edit|add mode for the table drivers CakePHP generates a nice drop-down with the content of the name field.
The issue:
The drop-drown shows only the ids instead of the content of the name field as long the table has the word companies in it. When I rename the table to transport_firms | transports | transport_units or something else then the drop-down field is populated correctly. I do NOT change anything, I only bake all models each time I rename the table.
My question:
Is there any CakePHP restriction regarding the word companies in a tablename because the drop-down isn't populated correctly?
Greg Schmidt's comment was the important hint. The CakePHP caches got messed up and - according to my another comment - another references began to fail as well.
I avoid this issue running these commands each time I make a change in the database:
bin/cake cache clear_all && bin/cake bake model all
As I don't add any code to the model/table and entity files I can always overwrite all of them. This simplifies the maintenance substantially.

How can i get cakephp current model's foreign key column name?

Is there way to get current model's foreign key name in other model something like this
echo $this->Category->whatIsMyForeignKeyName();
// expected output 'category_id'
Edit: Version
strtolower($this->Category->alias).'_id'
somehow seems not in spirit of cake.
If you know what kind of relation it is (belongsTo for example), you can easily look it up in the defined relations:
$foreignKey = $this->belongsTo['Category']['foreignKey'];
What you did with strtolower will work in almost all (or maybe all?) cases, though a slightly more thorough way would be something like:
$fkey = Inflector::singularize(Inflector::tableize($this->Category->alias)).'_id';
But the question I'd be asking is why do you want to do that? Why not just hard-code it as 'category_id'?
I had the same issue and I solved it in another way.
joshua.paling's solution finds you the default foreign key. Anyway, I think you can find it a step easier:
Inflector::underscore($baseModelName).'_id';
But as a matter of fact a model may have different foreign keys for each associations. Let's assume there is a "Base" and it has many "Associated" (these are dummy names for models). The relations is "hasMany" relation.
So your definition of "Base" model is the following:
class Base extends AppModel {
public $hasMany = array(
'Associated'
);
}
In the "BaseController" you can retrieve the foreign key that links these models together in the following way:
$this->Base->hasMany['Associated']['foreignKey']
This is the foreign key used for linking "Associated" rows to the relating "Base" row in the database.
I know this question is already answered more then a year ago but I hope my answer could help some other people :)

CakePHP makes a join between two models, but not between two others

I have four very simple models. Offer, Employer, Employee and Response.
Offer hasOne Employer
Employer hasMany Offer
Response hasOne Employee
Employee hasMany Offer
And now when I make a find() on Offer, it nicely makes a JOIN query and returns Employer details.
But when I make a find() on Response, it doesn't attempt to retrieve Employee's data.
I reviewed the code many times, stripped the models off of any additional properties etc., and still nothing. Those models are now nearly identical, their SQL tables too, but Response behaves like it has no relation to Employee defined.
Any pitfall with this that I might be trapped in? I'm ready to report this as a bug at this moment.
I can post complete (which are short, anyway) model definitions here if it helps anything.
Check the class of your model instances
I.e.
debug(get_class($this->Response));
If it outputs AppModel - the reason your code doesn't appear to be being used, is because: it isn't being used. That being the case, check for typos in the filename/location of your model files - as CakePHP will silently use an AppModel instance if your model files don't exist.

cakePHP HABTM, am I getting it all wrong?

I understood that every new row, causes the deletion of the rows that were there before?
What is the idea behind it? I don't believe that it is ..
So, what am i getting wrong?
Edit A
I have a form that adds a store to the Stores table. the store have a column named owner_id which is associated to the Users table through a belongsTo relationship.
There is also a table named stores_users that supposed to store the mangers for each store, using the HABTM relationship.
For this table there is a form with an email field, that connects the user to the store by saving the record directly to the stores_users table.
So, there is no full HABTM save anywhere, if I understand the term correctly.
So, my questions are:
Should I expect problems using it this way?
Can you advice me about how to it, if my method is not the proper way?
How can I use the stored data, using $this->User->find(...) to get all the stores that the user can manage?
yes, thats the default behavior of HABTM in cakephp
although this is not on "every row", but "every HABTM save".
this is working IF you always provide all HABTM values.
and with baked views according to the specifications for such HABTM this is all working out of the box.
if you change the default behavior (old ones get not deleted) you will need to make sure that there are no duplicates. there are behaviors out there, I think, which try to accomplish that.
but I would recommend for you to build your forms the way that the default behavior of cake can do its job.
Example:
IS: 1,3,6 (in DB for this key)
NEW: 2,3,6 (coming from form)
(cake deletes 1,3,6 and adds 2,3,6)
=> overall result (forgetting about different primary keys): "1" deleted, "2" added
so it might not be the most resource sparing way but its sure the easiest and fastest.

Resources