has_many through using custom table and field names - php

I'm using php.activerecord, and I am trying to link tables together. I'm not using their structure, but php.activerecord assumes I am, so it doesn't always work. I'm trying to use it on an already made app, so I can't change the database.
I learned from my previous question - Model association with custom table and key names - that I need to be as explicit as possible with the primary_key and foreign_key fields.
I'm having issues now using has_many through. I keep getting NULL, and I have no idea why.
So, here's a scenario: I have 3 tables, contacts, contactPrefs, and preferences. Those tables are as follows
contacts
--------
contactID
name
status
contactPrefs
------------
contactID
prefID
prefValue
preferences
-----------
prefID
name
description
Each contact has multiple contactPrefs. Each contactPrefs has one preferences. I tried to use has_many to get this working, but it's not. Here are my models:
Contacts.php:
<?php
class Contact extends ActiveRecord\Model {
static $primary_key = 'contactID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'primary_key' => 'contactid',
'class_name' => 'ContactPref'
),
array(
'preferences',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'through' => 'prefs',
'class_name' => 'Preference'
)
);
}
ContactPref.php:
<?php
class ContactPref extends ActiveRecord\Model {
static $table_name = 'contactPrefs';
static $belongs_to = array(
array(
'contact',
'foreign_key' => 'contactid',
'primary_key' => 'contactid'
),
array(
'preference',
'foreign_key' => 'prefid',
'primary_key' => 'prefid'
)
);
}
Preference.php:
<?php
class Preference extends ActiveRecord\Model {
static $primary_key = 'prefID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'class_name' => 'ContactPref'
)
);
}
According to the docs, I now should be able to the following:
<?php
var_dump(Contact::find(1234)->preference);
I cannot. I get NULL. Oddly, I can do this:
<?php
var_dump(Contact::find(1234)->prefs[0]->preference);
That works correctly. But, shouldn't I be able to access the preference object directly through the contact object? Am I misunderstanding the docs (they aren't the greatest, in my opinion)? Am I doing something wrong?

First you are reading the docs with a small flaw. In the docs you are shown:
$order = Order::first();
# direct access to users
print_r($order->users); # will print an array of User object
Which you are already doing via Contact::find(1234)->prefs. Let me boil it down a bit
$contact = Contact::find(1234);
# direct access to prefs
print_r($contact->prefs); # will print an array of ContactPref object
Second, what you actually want is undefined. What should Contact::find(1234)->preference actually do? Return the preference of the first ContactPref? Return an array of Preference objects?
I feel like offering both:
<?php
class Contact extends ActiveRecord\Model {
static $primary_key = 'contactID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'primary_key' => 'contactid',
'class_name' => 'ContactPref'
),
array(
'preferences',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'through' => 'prefs',
'class_name' => 'Preference'
)
);
public function get_preference() {
return isset($this->prefs[0])
? $this->prefs[0]->preference
: null
;
}
public function get_preferences() {
$preference=array();
foreach($this->prefs as $pref) {
$preference[]=$pref;
}
return $preference;
}
}
Let me explain a little bit what I have done. The ActiveRecord\Model class has a __get($name) function that looks for another function called get_$name, where $name in your case is preference (for the first result) and preference (for the entire collection). This means you can do Contact::find(1234)->preference which would be the same as doing Contact::find(1234)->prefs[0]->preference (but safer, due to the check) and Contact::find(1234)->preferences to get the entire collection of preferences.
This can be made better or optimized in numerous ways, so please don't take it as it is, but do try and adapt it to your specific situation.
For example you can either use the id of the preference as an index in the array or either not force a load of more data from ContactPrefs than the ones you are going to use and try a more intricate query to get the preference objects that you specifically need.
If I find a better implementation by getting through to work in the relationship definition, I'll return. But seeing the Unit Tests for active record, I'm skeptical.

There are several things that look strange, so it's not easy to come to a "this will fix it" for you, but this is an issue at least:
Fieldnames should always be lower-case in phpactiverecord. SQL doesn't mind it either way (not that table names ARE case-sensitive, but column names aren't). So make this:
static $primary_key = 'contactID';
into
static $primary_key = 'contactid';
The connections // find commands can be used in SQL, in which case it doesn't really matter how your key-string is 'cased', so some stuff works. But if the connection goes trough the inner-workings of phpmyadmin, it will fail. So check out this contactID but also the prefID.
Again, this goes only for COLUMN names, so don't go changing classnames or table-names to lowercase.
(extra point: phpmyadmin has trouble with combined primary keys. So while it might be ugly, you could add an extra row to your contactprefs table (if you don't allready have it) called id, to make that table actually have something to work with. It wouldn't give you much trouble, and it would help the activerecord library a lot)

Try the following:
<?php
var_dump(Contact::find(1234)->preferences);
The documentation says that with a has_many relationship, it should be referenced by a plural (http://www.phpactiverecord.org/projects/main/wiki/Associations#has_many_through). The Contact::find(1234) returns a Contact object which has multiple contactPrefs with their each Preference. In addition, in your Contact model, you specify the has_many as preferences .
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'primary_key' => 'contactid',
'class_name' => 'ContactPref'
),
array(
'preferences',
'foreign_key' => 'prefid',
'primary_key' => 'prefid',
'through' => 'prefs',
'class_name' => 'Preference'
)
);
Edit Through Modification:
Try the following Contact model
<?php
class Contact extends ActiveRecord\Model {
static $primary_key = 'contactID';
static $has_many = array(
array(
'prefs',
'foreign_key' => 'contactid',
'class_name' => 'ContactPref'
),
array('preferences',
'through' => 'prefs',
'class_name' => 'Preference',
'primary_key' => 'prefID')
);
}

Related

has_one and has_many in codeigniter

How can I use has_one and has_many in codeigniter 3.0?
I would like to create relations through tables like in Ruby on Rails.
I.E
Class User
{
has_many: comments
}
Class Comments
{
belongs_to: users
}
This is the most common usage, and is used in almost every project. There is a simple pattern to defining this relationship.
Post has a creator and an editor, which may be different users. Here's how to set that up.
Post
class Post extends DataMapper {
$has_one = array(
'creator' => array(
'class' => 'user',
'other_field' => 'created_post'
),
'editor' => array(
'class' => 'user',
'other_field' => 'edited_post'
)
);
}
User
class User extends DataMapper {
$has_many = array(
'created_post' => array(
'class' => 'post',
'other_field' => 'creator'
),
'edited_post' => array(
'class' => 'post',
'other_field' => 'editor'
)
);
}
A couple of things to note here.
The relationship is now defined by the relationship key on either
side, not the model name. This has now become the only way to look
up the relationship.
The key on one side of the relationship becomes the other_field on
the opposite side, and vice-versa.
Because we need a way to specify the difference between posts that
were edited and those that were created, we have to declare the
slightly unusual edited_post and created_post relationships. These
could have any name, as long as they were unique and mirrored on
Post.
http://datamapper.wanwizard.eu/pages/advancedrelations.html

Change Client id to Client Name with CakePHP

Okay, so I have a form in CakePHP that submits to a database. In this form it submits a field called client_id and it is stored in the database as such as well.
Then I have a view to allow me to view all of the invoices that have ever been created. To view the client responsible for the invoice, I can currently only see the id entered in the form by placing: <?php echo $invoice['Invoice']['client_id']; ?> in the view.
The invoices go to one database called: invoices
The clients name is not stored in the invoices table, just the id
The clients information is stored in one database called: clients
I want to be able to actually display out the clients real name in the invoices view rather than the client id.
I tried adding the following query to my index controller, But i'm not sure what to do after this or if this is even right.
$this->set('clients', $this->Invoice->query("SELECT * FROM clients WHERE id='89545'"));
In order to keep this question short in the first place, Please request specific code by commenting. i.e. Controller code, view code, etc... Thank you in advance.
Additional Thoughts
If I wasn't using CakePHP I could use something like the following, So I guess I just don't know how to put this into cakephp "language".
<?php
query($con, "SELECT name FROM clients WHERE id='$client_id'");
echo $row['name'];
?>
roughly!
Update
Here are my models
First one being the Client.php
<?php
class Client extends AppModel {
public $hasMany = array(
'Invoice' => array(
'className' => 'Invoice',
'foreignKey' => 'client_id'
)
);
}
?>
Second one being the Invoice.php
<?php
class Invoice extends AppModel {
public $belongsTo = array(
'Client' => array(
'className' => 'Client',
'foreignKey' => 'client_id'
)
);
}
?>
And finally, to get my invoices from the database, I am using the following inside: InvoicesController.php
public function index() {
$this->set('invoices', $this->Invoice->find('all'));
}
Have you properly set up your model associations? If so you should be able to do something like $invoice['Client']['client_name'] assuming you didn't use recursive = -1.
/edit: Ok I don't mind throwing some code out, but this is a fundamental concept you'll have to try to wrap your head around. Every model that is connected to another model has to have the association set up or things will be painful.
I am assuming a Client hasMany Invoice. So each invoice is specific to a client, and they can have multiple invoices (ex. October 2013 vs November 2013 invoice). From the CakePHP page we see:
class User extends AppModel {
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'user_id',
'conditions' => array('Comment.status' => '1'),
'order' => 'Comment.created DESC',
'limit' => '5',
'dependent' => true
)
);
}
So using that as our template, we end up with:
public $hasMany = array(
'Invoice' => array(
'className' => 'Invoice',
'foreignKey' => 'client_id'
)
);
And that goes in Client.php. As the inverse of hasMany is belongsTo, we have an Invoice belongsTo Client. Again, using the CakePHP page as the template, we end up with:
public $belongsTo = array(
'Client' => array(
'className' => 'Client',
'foreignKey' => 'client_id'
)
);
And that goes in Invoice.php. Once you set up those associations, whenever you do something like $this->Invoice->find('all'); or $this->paginate('Invoice');, with proper $recursive settings, Cake will grab the corresponding Client record. This allows you to do what I said before, something like $invoice['Client']['client_name'].
You can also use the $actsAs = array('Containable'); in the model in order to use the 'contain' directive in your controller's find method (instead of using recursive which most likely will get unwanted data on models with many relations)

FuelPHP Get "Not null violation" on foreign key trying to insert into related model

I'm trying to insert into the table of a model with multiple levels of HasMany relationships. Here's the breakdown so far
Customer->(HasMany)->Members->(HasMany)->Incomes
However, on when trying to insert into the Incomes table, I get a "Not null violation" with the foreign key from the Members table not being carried to Incomes. I know the most common problem is screwing up the $_has_many and $_belongs_to properties, but as far as I can tell they are fine. Plus, Just inserting into the Member table works fine so I know at least for the first layer it's working! The only thing I can think of is if since it's a second level down, it's screwing up because of that. Here's my code:
Relation Link (Member)
protected static $_has_many = array(
'incomes' => array(
'key_from' => 'id',
'model_to' => 'Model_Income',
'key_to' => 'member_id',
'cascade_save' => true,
'cascade_delete' => true,
),
);
Relation Link (Income)
protected static $_belongs_to = array(
'member' => array(
'key_from' => 'member_id',
'model_to' => 'Model_Member',
'key_to' => 'id',
'cascade_save' => true,
'cascade_delete' => true,
),
);
The Controller Code
// code to set up $customer
$customer->members[] = Model_Member::forge();
// set $member_vals here
$customer->members[0]->set($member_vals);
$customer->members[0]->incomes[] = Model_Income::forge();
// set $income_vals here
$customer->members[0]->incomes[0]->set($income_vals);
$customer->save();
The first problem that comes to mind is that you're setting relations on ->members and ->incomes (plural), instead of ->member and ->income (singular).
I also prefer to do relations separately, instead of chaining them like you do. So, if you already have relations set when you're adding a new Member to Customer, or a new Income to Member, my guess is that the index will not be 0.
What if you try the following instead:
$member = Model_Member::forge($member_vals);
$income = Model_Income::forge($income_vals);
$member->income[] = $income;
$customer->member[] = $member;
$customer->save();
Does it work?
Different suggestion:
Since it appears that you're following the table/column naming convention, try only this:
// Model_Member
protected static $_has_many = array(
'incomes'
);
// Model_Income
protected static $_belongs_to = array(
'members' // plural, not singular like you seem to have
);
If you want more help, you should definitely show how you have the Member/Income models and tables.

Accessing more than one model deep relationships in Lithium

Is it possible to access more than one model deep in a relationship in Lithium?
For example, I have a User model:
class Users extends \lithium\data\Model {
public $validates = array();
public $belongsTo = array("City");
}
and I have a City model:
class Cities extends \lithium\data\Model {
public $validates = array();
public $belongsTo = array("State");
}
and a State model, and so on.
If I'm querying for a User, with something similar to Users::first(), is it possible to get all the relationships included with the results? I know I can do Users::first(array('with' => 'City')) but I'd like to have each City return its State model, too, so I can access it like this:
$user->city->state->field
Right now I can only get it to go one deep ($user->city) and I'd have to requery again, which seems inefficient.
Using a recent master you can use the following nested notation:
Users::all( array(
'with' => array(
'Cities.States'
)
));
It will do the JOINs for you.
I am guessing you are using SQL?
Lithium is mainly designed for noSQL db´s, so recursiveness / multi joins are not a design goal.
You could set up a native sql join query and cast it on a model.
query the city with Users and State as joins.
you could setup a db based join view and li3 is using it as a seperate model.
you probably should split your planned recursive call into more than one db requests.
Think about the quotient of n Cities to m States. => fetch the user with city and then the state by the state id. => pass that as two keys or embed the state info. This would be acceptable for Users::all() queries aswell.
Example using Lithiums util\Set Class:
use \lithium\util\Set;
$users = Users::all(..conditions..);
$state_ids = array_flip(array_flip(Set::extract($users->data(), '/city/state_id')));
$stateList = States::find('list',array(
'conditions' => array(
'id' => $state_ids
),
));
You can set up relationships in this way, but you have to use a more verbose relationship definition. Have a look at the data that gets passed when constructing a Relationship for details about the options you can use.
class Users extends \lithium\data\Model {
public $belongsTo = array(
"Cities" => array(
"to" => "app\models\Cities",
"key" => "city_id",
),
"States" => array(
"from" => "app\models\Cities",
"to" => "app\models\States",
"key" => array(
"state_id" => "id", // field in "from" model => field in "to" model
),
),
);
}
class Cities extends \lithium\data\Model {
public $belongsTo = array(
"States" => array(
"to" => "app\models\States",
"key" => "state_id",
),
);
}
class States extends \lithium\data\Model {
protected $_meta = array(
'key' => 'id', // notice that this matches the value
// in the key in the Users.States relationship
);
}
When using the States relationship on Users, be sure to always include the Cities relationship in the same query. For example:
Users::all( array(
'with' => array(
'Cities',
'States'
)
) );
I have never tried this using belongsTo relationships, but I have it working using hasMany relationships in the same way.

ActiveRecord - associating foreign keys with different naming conventions?

I am working on a private messaging system using PHP-ActiveRecord ORM. I have models called 'User' and 'Message', MySQL tables called 'users' and 'messages', and in the messages table, I have fields called 'sender_id' and 'recipient_id'. However, I have no idea how to properly associate senders and recipients to the User model.
This is what I have so far in the User model:
static $has_many = array(
array('messages'),
array('recipients', 'foreign_key' => 'recipient_id', 'class_name' => 'Message'),
array('senders', 'foreign_key' => 'sender_id', 'class_name' => 'Message'),
);
and this is what I have so far in the Message model:
static $belongs_to = array(
array('sender', 'class_name' => 'User'),
array('recipient', 'class_name' => 'User'),
);
However, when I run the code such as $message->recipient->first_name, it does not properly pull the first name from the User model, like I want it to. I am not sure what I am doing wrong.
Normally, I just use the standard naming conventions. However, for this example, we have to have sender_id and recipient_id, which are both the same type, so I can't just use the standard naming conventions as I have been doing.
Any help would be highly appreciated. I am using the PHP-ActiveRecord ORM. I think the Rails version is more widely used, but it is my understanding that they work the same except with different syntax.
You also need to specify the foreign keys in the $belong_to, otherwise they will be inferred from the table's name (i.e. user_id):
static $belongs_to = array(
array('sender', 'class_name' => 'User', 'foreign_key' => 'sender_id'),
array('recipient', 'class_name' => 'User', 'foreign_key' => 'recipient_id'),
);

Categories