Koahan ORM: Adding extra data to many-to-many relationships - php

I have my models setup as a many-to-many using through with a pivot table. However, I would like to add some extra data into the pivot table.
In the past (kohana 3.0) I was able to provide extra data with the add method
$obj->add('alias', $related, array('extra'=>'data'))
But its seems in Kohana 3.3 that the add method does not provide the third parameter for extra data, and I cannot seem to find how to do this short of after saving, adding more data then re-saving.

This isn't supported anymore since Kohana 3.1.
The reason they removed it (Source: http://dev.kohanaframework.org/issues/3754):
We decided to remove this because it's better to use a through model
if you need to put data in your pivot table. Inserting the data
directly in the add() method bypasses validation and filtering that
would normally be in your model. Use a model if you need data in your
through table. We won't be changing this.
You now have to make a model for the pivot table and place the additional information in that model.
Then instead of using has_many "through" (n:n) you should use has_many (1:n) relationship for both tables to the pivot table.
I hope this answers your question.

Related

How to save relations (many-to-many) between local data and an API?

I'm looking for a proper way to handle and store relations between data in my DB and data from a third party API. I use Laravel 5.
For example, I have a Project model (id, name). Also, I have an API which I can call from PHP and get back a JSON with Articles list.
I need to save a many-to-many relation with a junction table between Projects and Articles.
Because Articles data comes from API I do not have a local table with Articles so I can't use Eloquent to deal with relations.
My question is how to do it right.
I've thought of 2 possible solutions:
1.) Use some lib that can "map" an API as an Eloquent model. But I've only found abandoned projects. And overall this solution looks like overkill in such simple situation.
2.) Use query builder and manually handle this situation to save data about relations between Projects and Articles in the junction table.
But if I'll use this option it will be hard to deal with updates etc.

Laravel - how to delete polymorphic relations of model with belongsTo relation?

I have 4 tables. Two of them are monomorphic and two of them are polymorphic.
Monomorphic:
Templates, Modules
Polymorphic:
Documents, Images
Now, templates and modules have both documents and images and each Template has many Modules and modules have foreign key that is set to cascade on deletion of templates.
Now, if I delete a Template the associated Modules will be deleted but the associated polymorphic relations of Module will stay in Database. I haven't tried anything because I am completely clueless.
Anything I could do to automatically delete associations of Module when Template is deleted? I think the deletion here in this is being handled by Database itself and Eloquent doesn't have anything to do with it.
Because it is a polymorphic relationship, the cascading delete cannot be handled by a foreign key.
If you still want the deletes to be handled at the database level, you will need to write some type of database trigger functionality.
There are also a couple options if you want to handle this at the application level.
One option is to update your code so that everywhere you delete your Template, you also make sure to delete all of it's polymorphic relations.
Another option would be to create an event handler for the deleting event. The deleting event is fired by Eloquent whenever you delete an Eloquent model. Inside this deleting event, you can access the polymorphic relationship and delete them, as well.
If the deleting event method sounds okay, there is a package that implements all of this for you, and makes it very easy to setup for your models: shiftonelabs/laravel-cascade-deletes. Full disclosure: I wrote the package.
With this package, you just add the CascadesDeletes trait to your Template model, and then add a $cascadeDeletes property that contains an array of all the relationships to delete when a Template instance is deleted.
It's not fully automatic, but I handle it very easy with the destroy() Eloquent method. https://laravel.com/docs/8.x/eloquent#deleting-an-existing-model-by-key In addition, when you will use it with Laravel Queues, it works like a charm...
Example:
$template = Template::find(1);
Module::destroy($template->modules);
$template->delete();
You can actually use delete method on both types of relationship:
$template = Template::find(1);
$template->modules()->delete();
$template->images()->delete();
$template->delete();
Key note here is that $template->modules returns a collection instance while $template->modules() return an eloquent relationship where you can chain delete() method.

Many to Many Relationships

I'm seeing all sorts of different ways to handle many to many relationships in Yii. However, the examples I'm seeing aren't fully fleshed out and I feel like I'm missing something.
For instance, Larry Ullman's tutorial doesn't use the self::MANY_MANY relation - http://www.larryullman.com/2010/08/10/handling-related-models-in-yii-forms/
So as far as adding and retrieving records, whats the standard way for handling Many to Many in the Model, Controller and View?
clarification: I suppose Im looking for an example involving 2 tables, related by many-to-many, where I can see not only both models, but controllers and views as well so I can fully understand whats going on.
You actually need the 3 tables (so an extra model for the pivot table), but once you have it you can actually use the regular yii relation functionality.
For example a project of mine has a many-to-many relation between a Purchase and a Transaction (please don't ask why :) ). So there is a Purchase model, a Transaction model and a PurchaseToTransaction model that establishes links between them. I can just do a $oPurchase->transactions and Yii will handle the many-to-many part using the relation, it is defined as follows:
'transactions' => array(self::MANY_MANY, 'Transaction', 'PurchaseToTransaction(purchaseId, transactionId)')
Note that for the Transactions, the same applies but the other way around:
'purchases' => array(self::MANY_MANY, 'Purchase', 'PurchaseToTransaction(transactionId, purchaseId)'),
So both $oPurchase->transactions and $oTransaction->purchases are automatically handled by Yii.
In conclusion it can indeed handle Many-to-Many using relations (at least the reading part, for writing you still need arbitrary solutions).
I am just using Yii. Just a basic idea. If you thought One to Many is not a problem then you need to create a middle table in between like for Users and Orders, create a UsersOrders table to map those keys. Then create function to get those related tables in the class like $this->UsersOrders->Orders() for function Orders in User class, vice versa.
Many to Many is actually groundup by 3 tables. 2 tables but plus hidden table in the middle.

Yii save data to different tables

I am new to Yii. I need to save data collected from a single form to three different tables.
So my doubts are
How can I design the Model class (CformModel or CActiveRecord)??
How to design the view??
In Controller how can I save the data to different tables??
I need to manually validate some vales like md5 hash etc
you need to create three models. And use according model fields and save all three models.
In Yii one table - one model.
In your controller: saving your different models for different tables will look like:
$modelB=new Addresses;
$modelB->attributes=$sess['addresses'];
$modelB->save();
$modelC=new TenQs();
$modelC->attributes=$sess['tenqs'];
$modelC->save();
To render multiple models to one form you just keep listing the models in the render statement.
$this->render('create',array('modelB'=>$modelB,'modelC'=>$modelC));
That would work in your controller. This example assumed Active Record.

Yii framework Many to Many relationships

What is the method to save and update Many to Many relationship in Yii framework?
There is a better implementation as behavior.
http://www.yiiframework.com/forum/index.php?/topic/6905-please-test-my-ar-enhancement-automatically-sync-many-many-table-when-calling-save/
Unless you create a model for the table between the two main tables, your only option is to use DAO (Database Access Object) and specify SQLs with it.
Have a look at how blog demo accomplishes this task.
use MANY_MANY relationship type to setup many to many connection between Models (An associative table is needed to break a many-to-many relationship into one-to-many relationships)
And now you can use all relational functions of Active Records
Yii Framework - The Definitive Guide to Yii: Working with Databases-Relational Active Record
The following extension does what you want...
Yii Framework - Extension: cadvancedbehavior
An important thing to note: On each update, the extension clears all previous records and creates new ones. So I wouldn't use it when the intermediatry table contains extra data other than the foreign keys.
you could set that up in mysql level..by going to relational view under each table in phpmyadmin and provide necessary relational condition..and use MANY_MANY in the model class inside relations..
The question is too common.
Usually data components with MANY to MANY relationships appear sequentially and independently. So you just need to do one insert action after another.
If your relationship needs dependent update you should user SQL triggers on the DataBase level. That'll ensure integrity of data and give a quite good separation in business logic of the application.
CREATE TRIGGER some_trigger
AFTER UPDATE ON some_table
...
END IF;
A similar way is to incapsulate relational data in one logical model on PHP level (and e.g. manipulate with 2-3 AR models there) and emulate SQL triggers logic in it.

Categories