CakePHP - HABTM with predefined Model - validation

I have two models: Bar HABTM Foo. Now I have bars_foos join table with some additional fields.
When I add new Bar with some Foo associations or vice versa, I need to run many validation checks, whether the new associations are kosher. Checks are based on the additional fields defined and already saved associations.
Where should I run these validations? In Bar/Foo controller? Or should I create BarsFoo model with validation rules?
When I keep HABTM relationship to get all the auto-magic from Cake, I cannot find a way to tell Cake to use my own predefined BarsFoo model with validations rules. Cake creates its own virtual model and ignores mine.
Or should I (in this specific case) break HABTM into hasMany-belongsTo-hasMany relationship, where I can use my own BarsFoo model?

You can define your join model using the with key and create your validations there:
public $hasAndBelongsToMany = array(
'Bar' => array(
…
'with' => 'BarFoos'
)
);
http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

Related

using associate method in laravel

Assume we have one to many relationship like post and comment, each post hasMany comments and each comment belongsTo one post.
The question is how we could call associate method?
in Laravel document the associate method is called on relation method, something like:
$comment = new Comment(['title' => 'something']);
$post->comments()->associate($comment);
but in some cases, I saw the associate method is called on relation attribute like:
$comment = new Comment(['title' => 'something']);
$post->comments->associate($comment);
Are they the same? is there any difference between them?
Thanks
I don't think calling associate on a loaded relationship property works as it's either a database collection or a model (depends on the type of the relation). The method associate is only available on BelongsTo and MorphTo relationships.

How do sorting extbase repository Objects by mm.sorting

I have a TYPO3 Extension with two Model, Member and Category and M:M relation between this models.
Now I write a special repository method findByCategoryUid to get members sorted by Date.
How can I sort/order the Member Objects by the field tx_***member_membercategory_mm.sorting ?
This one dosn't work.
$query->setOrderings(
array('tx_***_member_membercategory_mm.sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
);
When working with the Extbase repositories/queries you should forget about the mysql tables and just look at your models. To sort by a related field you have to use your model fields. If your categories are in the field category in the Member model, than you need to use category.sorting as the sorting field. The same goes for filtering in the query.
$query->setOrderings(
array('category.sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
);
It only works if you configured the TCA and your models right.

Laravel change stored value of polymorphic relationship

I am trying to make the stored value of a polymorphic relationship more readable by other applications. Currently the polymorphic model type is stored as the FQCN of the model. Using the example in the Laravel Docs, imageable_type could be "App\Product", or "App\Staff". However, this value can be a little more difficult to manage if any non-laravel applications which aren't based on this convention and are also accessing the same database. Also, if the model FQCN ever gets refactored, you have to modify your other applications to account for the change.
Is there a way to change the type to something more consistent and readable, and then have a mapping class that maps the keys to the model? (e.g. have "product" map to "App\Product")
Yes. This is a change that was recently implemented.
Add this to your service provider (in the boot method):
Illuminate\Database\Eloquent\Relations\Relation::morphMap([
'product' => App\Product::class
]);
If you simply pass an array of model names, it'll default to using the table names:
Illuminate\Database\Eloquent\Relations\Relation::morphMap([
App\Product::class,
App\Staff::class,
]);
if you are adding morphMap method to service provider, you might want to use
'product' => \App\Product::class
( "\" before App),otherwise your namespace can be wrong.

Can I set a generic yii view string based on model instead of individual widgets

I have set up relationships between several tables. I got the view in tableFoo to look up the Name in tableBar instead of showing barId, by putting the following into the widget:
array( 'label' => $model->Bar->getAttributeLabel('Bar'),
'value' => $model->Bar->Name ),
But I have lots more tables, and I'd prefer to have a single place for setting these things. Is there a way to set anything with a relation to Bar to show Bar->Name? Or do I have to do this separately for all the barIds, bazIds, etc throughout the model?
tldr: Where is a single point of truth for displaying Name instead of ID for all related tables?

Relations function in AR Model, many to one relationship

So here's the scenario:
I have two table, Issue & Project.
A Project can have many Issue and an Issue can exactly one project.
Since Issue is many to one, do you have to define it?
Cause I know in Project Model I have:
public function relations()
{
return array(
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
);
}
For Issue Model I have nothing but foreign keys:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
);
}
I'm guessing anything to one relationship does not need to be define?
Thank you in advance.
BTW, I'm doing agile Yii book and I ended up asking myself this question.
There's a has-one option in AR class (http://www.yiiframework.com/doc/guide/database.arr).
But is this case optional for some reason?
It helps me to think of the difference between BELONGS_TO and HAS_ONE as "where is the foreign key stored"? If the Project model stored "Issue_Id" then potentially Issue could have many Projects. You use the HAS_ONE relationship to state that even if Issue COULD have many projects, it only has ONE.
However, the more common case is if you are storing the Project_Id in the Issue model (and I assume you are). Then you have to use the BELONGS_TO relationship. It appears you have defined the relationships correctly above.
Someone posted a similar question relating to Yii relations here that I helped answer:
yii - using relation HAS_ONE to get data from the related table to display in list page
As to your concern about "needing" to define relationships, you don't "need" to define any. You could write your own SQL queries to do the same thing. The ActiveRecord relations are just a convenience thing to make querying for related records simpler. If you are never going to look up an Issue's Project, then you don't "need" to define the "project" BELONGS_TO relationship.
Without actually seeing your database structure it looks to me like you have everything set up correctly. You can now easily make both $issue->project and $project->issues "lazy" relational queries, taking full advantage of the power of the Relational Active Record. Cheers and good luck with project!
In your Issue model you already have the relation specified as BELONGS_TO the project model.
I also just got the new yii-book. It helped me alot!
Happy coding :)

Resources