Kohana 3 ORM Relationships Question - php

I've been through several sites (including this one), and unfortunately as a Kohana newbie I still can't get this to work. The data relationship is fairly simple, I have a company record, which should be linked to 1 status record and 1 type record. Of course there will be multiple companies in the table, but each company is only allowed to be linked to 1 of each (and must be).
What I have is:
class Model_Company extends ORM
{
protected $_has_one = array(
'companystatus' => array('model' => 'companystatus', 'foreign_key' => 'entryid'),
'companytype' => array('model' => 'companytype', 'foreign_key' => 'entryid')
,
);
}
Company Status Model:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_CompanyStatus extends ORM
{
protected $_table_name = 'datadictionary';
protected $_primary_key = 'entryid';
protected $_has_many = array(
'company' => array('foreign_key' => 'statusid')
,
);
}
?>
Company Type Model:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_CompanyType extends ORM
{
protected $_table_name = 'datadictionary';
protected $_primary_key = 'entryid';
protected $_has_many = array(
'company' => array('foreign_key' => 'companytypeid')
,
);
}
?>
The companystatus and companytype models are mapped to a single table which has 2 fields, entryid and entryname. This table is called "datadictionary", and has the appropriate properties so that I don't have to use "id" as the record id field.
Now I load my Company record like this:
$company = ORM::factory('company')
->where('id', '=', 1)
->where('hasbeendeleted', '=', 0)
->find();
The problem is that I don't get anything back for the companystatus and companytype properties for the company, and when I do a $company->companystatus->find() I get the first record returned, which is weird. What am I missing?
Thanks!!
:-)
Edit:
For simplicity's sake the Companies table has the following fields:
ID (primary key) - auto inc int
CompanyName - varchar(255)
StatusID - int
CompanyTypeID - int
HasBeenDeleted - smallint (0 for false, 1 for true)
DataDictionary Table:
EntryID (primary key) - auto inc int
EntryName - nvarchar(255)
Example Company record:
ID: 1
CompanyName: TestCompany
StatusID: 1
CompanyTypeID: 3
HasBeenDeleted: 0
Example DataDictionary records:
EntryID: 1
EntryName: Active
EntryID: 2
EntryName: Inactive
EntryID: 3
EntryName: Customer
EntryID: 4
EntryName: Supplier

There are a few things here I would try changing.
First of all, for readability, most people use underscores in foreign keys. So instead of entryid, I'd recommend using entry_id (you'd have to make the change in both your database and your code).
In Kohana 3, declaring 'model' => 'companystatus' in a $has_one array is redundant when the key is the same as the model name. You can safely remove that part.
But really, that's all incidental to your problem, which exists somewhere between that last ORM call and your database. (I'm assuming here that hasbeendeleted is a column in the company table, not either of the other two tables you mentioned. Let me know if that's not the case.)
If you're doing a ->where('id', '=', 1) together with a ->find(), you're really expecting to return the one company record if it exists in the database. I would recommend making a separate check for hasbeendeleted.
And speaking of which, instead of naming that variable $companies, it should really be singular (e.g. $company) since it will only hold one record.
And you can simplify ORM::factory('company')->where('id', '=', 1) to simply ORM::factory('company', 1)
If you know for sure that a company with a database ID of 1 exists, then the following code should return that record:
$myCompany = ORM::factory('company', 1);
Then you can do something like if ( ! $myCompany->hasbeendeleted) ...
That should help you a bit. Post more details if you run into trouble.

Related

Cakephp 3: Set virtual property with condition on belongsToMany association

I have two tables, Tickets and Paintings. A ticket can have many paintings and a painting can be used on many tickets. They have a join table called tickets_paintings with ticket_id and painting_id. Here's how the tables are set:
class TicketsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Paintings', [
'foreignKey' => 'ticket_id',
'targetForeignKey' => 'painting_id',
'joinTable' => 'tickets_paintings'
]);
}
class PaintingsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Tickets', [
'foreignKey' => 'painting_id',
'targetForeignKey' => 'ticket_id',
'joinTable' => 'tickets_paintings'
]);
}
Every Ticket has a field "active" that is boolean and tells me if the ticket is currently in use or not. In the edit function the field can be changed to true or false.
But now I also need a property for every Painting, that tells me if this painting is currently used on an active ticket. In this case it would be unavailable to be used on another ticket.
I thought I could add a boolean virtual property for a painting, that checks if it is (currently) associated with any ticket that has active => true and is therefore also set true or false.
Like: "Does this painting belong to a ticket with state active => 0".
And then set the virtual property accordingly, so I can display it in a view.
I managed to create a virtual property for a painting that checks if there is an entry for its ID inside the join table so far:
class Painting extends Entity
{
protected function _getIsAvailable(){
$TicketsTable = TableRegistry::get('TicketsPaintings');
$exists = $TicketsTable->exists(['painting_id' => $this->id]);
if($exists == true){
return 1;
} else {
return 0;
}
}
}
A) How could I add a condition that checks if any matching ticket_id in the join table has an active=> 1 in the original Tickets table?
The query would have to check the ticket_id in the join table in an DESC order and return true as soon as the first active ticket is found (so it does not check the whole thing every time)
I don't even understand how to access that "active" property on the tickets table to check it. I assume I would have to use the " has Many through" option?! But even trying to following the explanation in the book and this question exactly (for hours), I am not able to get this to work for my example because I don't understand the correct syntax I had to use for my tables.
B) Is it even possible or "advisable" to check sth like this inside the Entity?
I tried to use an extra db field in the paintings table first but it seemed much more complicated to check and set another value every time a ticket is edited (not that I managed to make that work either)...and the active value is pretty much doubled then for ticket and painting. So I thought the virtual property would be easier to handle. Or am I on the completely wrong train?
Thanks for any tips or advice on this nightmare =)!
Since nobody came to my rescue I managed to put something together and actually create the virtual property I wanted. I don't know if this is the best way to go but it works for me and I will put it here in case anyone is searching for similar case.
I created a finder method inside the Paintings Table that joins the 3 tables and filters out all paintings that are connected to an active ticket.
class PaintingsTable extends Table
{
public function findNotavailable(Query $query, array $options){
$query
->join([
'tickets_paintings' =>[
'table' => 'tickets_paintings',
'type' => 'LEFT',
'conditions' => 'tickets_paintings.painting_id = Paintings.id'
],
'tickets' => [
'table' => 'tickets',
'type' => 'LEFT',
'conditions' => 'tickets_paintings.ticket_id = Tickets.id'
],
])
->select(['Paintings.id'])
->order(['tickets_paintings.ticket_id' => 'DESC'])
->where([
'Tickets.active' => false
]);
return $query;
}
}
Then in the Entity of the Painting I check if its ID is inside this selection and set a boolean virtual property accordingly:
use Cake\ORM\TableRegistry;
class Painting extends Entity
{
protected function _getNotAvailable(){
$Paintings = TableRegistry::get('Paintings');
$notavailable = $Paintings->find('notavailable')
->where(['Paintings.id'=>$this->id]);
if($notavailable->isEmpty()){
// if no record --> painting is available
return 0;
} else {
// if record found --> painting is not available
return 1;
}
}
}
And in the view I display it like this:
In Paintings - view.ctp
<?= $painting->not_available ? __('not available') : __('available'); ?>
The only downside to it is that virtual properties can't be used for pagination as far as I understand the book. But I'm sure there's a workaround for that too.

Yii2 - Gii Model Relations - Why is there a 0 after the function names?

I haven't seen this online when looking at other's code, guides, tutorials, etc.
When I generate a Model with Gii, the functions regarding relations all have a zero after them.
Example:
class Benefit extends \yii\db\ActiveRecord
{
// truncated Yii Model code...
public function getType0()
{
return $this->hasOne(BenefitTypes::className(), ['id' => 'type']);
}
}
BenefitTypes is an id to name mapping:
id | name
---------------
1 => Federal
2 => Non-Profit
In the 'benefit' table, it has column named 'type' that is a relation to the 'benefit_types' table 'id' column.
I though I should be able to do (in /views/benefit/index.php) 'type.name' but it doesn't work either. It changes the column name to "Type Name" and puts "(not set)" in the data table...
Example:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'somevalue',
'type.name',
],
]) ?>
What is going on, why does it not act like it's supposed to?
UPDATE
I am beginning to think the 0 suffix to the relation function names, ie: getType0, is due to "type" being used in the table as a column name to avoid duplication or confusion. I can't find this documented though, so would like to have a definite answer on that.
I changed the function name to getTypeRelation(). Then in the index.php view, for the detailview widget, used 'typeRelation.name' and it returned the name through the relation just fine.
Your thinking is correct. Generation of the relation names is done by the function generateRelationName().
protected function generateRelationName($relations, $table, $key, $multiple)
{
if (!empty($key) && substr_compare($key, 'id', -2, 2, true) === 0 && strcasecmp($key, 'id')) {
$key = rtrim(substr($key, 0, -2), '_');
}
if ($multiple) {
$key = Inflector::pluralize($key);
}
$name = $rawName = Inflector::id2camel($key, '_');
$i = 0;
while (isset($table->columns[lcfirst($name)])) {
$name = $rawName . ($i++);
}
while (isset($relations[$table->fullName][$name])) {
$name = $rawName . ($i++);
}
return $name;
}
Yii uses the related table's name as the relation name. Should you have a column with the same name as the related table, a digit will be appended to the relation to avoid confusion due to Yii's handling of magic functions. This also occurs if you have two columns or more in a single table related to the same table e.g columns create_user_id, update_user_id and delete_user_id related to table user will result in relations named user, user0 and user1.
For your example, it is advisable to name your foreign key field something else e.g type_id or typeId. Yii will handle these correctly . The other alternative when you have multiple columns related to the same table is to just rename the functions.
Because relational column name and the relation name is same. When you call $benefit->type what you will expect, the value of the column/property type or the instance of BenefitTypes? So now you know. $benefit->type return the property value and $benefit->type0 returns the instance of relation.

Creating new record and relationships in one go

I have the following basic schema:
players
id
name
profiles
id
player_id
email
subsets
id
profile_id
alias
I was under the impression the following operation was possible when creating a new record:
Player::create([
'name' => 'Player 1',
'profile.email' => 'player1#email.com',
'profile.subset.alias' => 'Player 1 alias'
]);
Since this code doesn't seem to work, is there anyway to save relationships records together with the create method?
Basically, you can't do this as easy as it looks.
In the docs, all related models are created after the base model is created
$profile = new Profile(array('email' => 'player1#email.com','alias'=>'Player 1 alias'));
$player = new Player(array('name'=>'Player 1'));
$player = $post->profile()->save($profile);
However , if you really want to do it in one go, you can overwrite the save() method in the Player model :
public function save(){
Database::transaction(function() {
$profileModel = new Profile($this->profile);
parent::save();
$this->profile()->insert($profileModel);
});
}
You will then pass the array to the Player method like :
array(
name='Player name',
profile=>array(
email=>'player1#email.com',
subset=>array(
alias=>'Player 1 alias'
)
);
Although this is not a recommended action.
Please read more about how to save the Eloquent models and relationships :
Tutorial 1
Tutorial 2
Everywhere is suggested to create the base model first, then the related models.

Order data based on count of related table data

I have Two diff tables as given below:
users and posts
Need data from user's table order by count of posts table
Relationship is defined as:
User Model:
public $hasMany = array('Post');
Post Model
Public $belongsTo = array('User');
counterCache - Cache your count()
This function helps you cache the count of related data. Instead of counting the records manually via find('count'), the model itself tracks any addition/deleting towards the associated $hasMany model and increases/decreases a dedicated integer field within the parent model table.
The name of the field consists of the singular model name followed by a underscore and the word “count”:
my_model_count
Let’s say you have a model called ImageComment and a model called Image, you would add a new INT-field to the image table and name it image_comment_count.
Once you have added the counter field you are good to go. Activate counter-cache in your association by adding a counterCache key and set the value to true:
<?php
class Image extends AppModel {
public $belongsTo = array(
'ImageAlbum' => array('counterCache' => true)
);
}
From now on, every time you add or remove a Image associated to ImageAlbum, the number within image_count is adjusted automatically.
You can also specify counterScope. It allows you to specify a simple condition which tells the model when to update (or when not to, depending on how you look at it) the counter value.
Using our Image model example, we can specify it like so:
<?php
class Image extends AppModel {
public $belongsTo = array(
'ImageAlbum' => array(
'counterCache' => true,
'counterScope' => array('Image.active' => 1) // only count if "Image" is active = 1
));
}

Relational Databases in Yii

So I've tried this: http://www.yiiframework.com/wiki/285/accessing-data-in-a-join-table-with-the-related-models
Basically I have a table called User which relates to ToolAccess; related via a primary key on User and a field for userID on ToolAccess. Now tool access relates to the table Tool which contains a ToolID. Now this doesn't work in Yii, I can't seem to get the toolName field off of the tool table using Yii. Any ideas on how to do this on a Active Record?
I'm using giix if that matters.
Relations code:
public function relations() {
return array(
'usergalleries' => array(self::HAS_MANY, 'Usergallery', 'userid'),
'userinroles' => array(self::HAS_MANY, 'Userinroles', 'userid'),
'userfailedlogin' => array(self::HAS_MANY, 'Userfailedlogin','userid'),
// table name, relation, class name, relation key
'toolaccess' =>array(self::HAS_MANY, 'Toolaccess','userid'),
'tool' =>array(self::HAS_MANY, 'Tool','toolid')
);
}
I'm assuming your schema looks something like this:
User table tool_access table Tool table
id | other columns userid | toolid id | name | other columns
In this case, the User model should have a relation like this (note that the tools will be ordered by name in this case):
public function relations() {
return array(
// other relations here...
'tools' =>array(self::MANY_MANY, 'Tool', 'tool_access(userid,toolid)',
'order' => 'tools.name',
),
);
}
and the code to read the tools should look like this:
$user = User::model()->with('tools')->findByPk($id);
foreach($user->tools as $tool) {
echo $tool->name;
}
I used eager loading of the tools here mostly because of personal preference, using lazy loading should work just as well in this case. But eager loading should be preferred whenever you're processing multiple User records at once.
So if I have understood it properly, user and tool are related in a many-to-many relationship by their primary keys.
So you should define this relationship in the User model like:
'tools' => array(self::MANY_MANY, 'Tool', 'tool_access(userid, toolid)', 'index' => 'id'),
This way you can access the name of the tool after getting the user model
$user = User::model->findByPk($id);
$tools = $user->tools;
foreach ($tools as $tool)
{
echo $tool->name;
}
I hope it works for you.

Categories