Module to Module DB relations - yii

In Yii framework v1.1.13
I have Module "A" that has model class "M1" which is related to "M2" in Module "B".
Now my question is, How to connect Foreign keys between the two models.
I want the correct reference to M2 in Module B
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(
'M2_property' => array(self::BELONGS_TO, 'B.M2', 'M2_id'),
)
}
thanks in advance

No need to connect the way u have done let gii generate its code for relations . Only thing u have to do is import all your module files in config main.php for example u have two modules say M1 and M2 and models in each m1 and m2 u just import as shown below
'import'=>array(
'application.modules.M1.components.*',
'application.modules.M1.models.*',
'application.modules.M2.components.*',
'application.modules.M2.models.*',
),
The two modules will be related to each other

Related

Displaying data from a normalised m-m relationship

I have have 2 tables, ServiceProvider , Entity. There is a m-m relationship between them so I created a link table spEntity which contains the foreign keys from both the other tables.
I have backpack working fine on the 3 tables separately, I use the relationship type on the spEntity CRUD to show the names from the other 2 tables and this all works fine.
However, what I would like to do is when the ServiceProvider record is being created
show a list of all the entities
allow the user to select one and then when the save button is pressed
create the spEntity record.
I have tried
protected function setupCreateOperation()
{
CRUD::setValidation(ServiceProviderRequest::class);
CRUD::field('ServiceProviderName');
CRUD::field('ServiceProviderEmailAddress');
CRUD::field('ServiceProviderDescription');
CRUD::addfield(['name'=>'Entity',
'type'=>'relationship',
'attribute' => 'EntityName',
'pivot'=>true,
'relation_type'=>'belongstomany'
]);
}
but I get the error :
BadMethodCallException PHP 8.1.7 9.19.0
Method Backpack\CRUD\app\Library\CrudPanel\CrudPanel::relationAllowsMultiple does not exist.
Table relations
Is this possible?
Try updating laravel-backpack/CRUD because seems to be an old bug.
Partial solution.
The problem was in my model definition. I need to add in the m-m relationship there.
Model
public function entities()
{
return $this->belongsToMany(Entity::class,'SPEntity','Entity_idEntity','ServiceProvider_idServiceProvider')
->withPivot('idSPEntity')
->withTimestamps();
}
then in the Crud controller
CRUD::addfield(['name'=>'entities',
'type'=>'select2_multiple',
'attribute' => 'EntityName',
'pivot'=>true,
'relation_type'=>'belongstomany'
]);
this now displays the field correctly and I can add further entities to the Service Provider.
However the information is not saved.

overriding already overridden classes with a module - Prestashop 1.6

I KNOW THIS QUESTION IS NOT SPECIFICALLY TIED TO PROGRAMMING. BUT I REALLY WANT TO UNDERSTAND PRESTASHOP'S BEHAVIOUR OF OVERRIDING FILES VIA MODULES.
I want to extend a MYSQL table, let's say, Orders. A raw sql query in Module's install method would get this, no problem. But I wanna add this column to Order Model as well, which is where the main problem would come. Since I must override it like this:
public function __construct($id = null, $id_lang = null)
{
parent::__construct($id, $id_lang);
self::$definition['fields']['new_column'] = array('type' => self::TYPE_STRING);
}
My question is, how do I make sure that if override directory already has an Order.php with a __construct, how do I make it to merge changes rather than throwing error.. Is it possible?
Prestashop doesn't encourage using of overrides within modules at all. Because how I know there is no way to merge two overrides and only the last one will work if you will find a way to install a module with the second override. So they recommend extending existing classes and add all necessary data from there. For example, in your case, it should be something like this
class Order extends OrderCore
{
public $new_filed;
public function __construct($id = null, $id_lang = null)
{
Order::$definition['fields']['new_column'] = array('type' => self::TYPE_STRING);
parent::__construct($id, $id_lang);
}
}
and just include this class file inside your module. So if something similar will be included within another module no conflict should appear except if the properties will have the same name.

How to setup yii model relations right, is using 't' to refer to default table a good idea

Usage of 't' in model relations can give the error
"Column not found: 1054 Unknown column 't.etc' in 'on clause'."
It's the usage of 't' to refer to the current table in CActiveRecord Model relations.
I often stumble accross it when using ,findAll, CActiveDataProvider, etc
Sometimes it works sometimes it doesn't depending on what model you execute and from where.
I tried using tableAlias but it doesn't work. There must be an easy way.
How can I setup my models and it's relations in such a way that the relations are stable
and always works?
Here is an example of two classes to show the problem...
class Order extends CActiveRecord
{
/**
* #return array relational rules.
*/
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(
'author'=>array(self::BELONGS_TO, 'User', 'user_id'),
'shopproduct'=>array(self::BELONGS_TO, 'ShopProduct', 'product_id',
'with'=>array(
'tagsrelations',
),
),
);
}
}
class ShopProduct extends CActiveRecord
{
/**
* #return array relational rules.
*/
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(
'author'=>array(self::BELONGS_TO, 'User', 'user_id'),
'tagsrelations'=>array(self::HAS_MANY, 'TagsRelations','',
'on'=>' tagsrelations.tbl_uuid = t.uuid ',
'with'=>'tag',
'order'=>'tag.name ASC',
),
);
}
}
// works
$model=Order::model()->with('shopproduct')->findAll();
// doesn't work
// "Column not found: 1054 Unknown column 't.uuid' in 'on clause'. The SQL statement executed was"
$model=ShopProduct::model()->with('tagsrelations')->findAll();
Maybe someone can explain why this isn't working in an understanding way.
How to fix this one once and for all. BELONG TO relations usually work.
What is the best to make an HAS MANY relationship that always works
Do you need the t only for the join or for some specific purpose? Because for the join this is not the right way to do it. It should be done sth like this:
'tagsrelations'=>array(self::HAS_MANY, 'TagsRelations', array('tbl_uuid'=>'uuid')),
Of course you can then add the with condition if you need that as well. But the join should be done this way.

Fluent 1.2 Join statement not working

I am trying to get fluent 1.2 to work on a project that currently uses v1.1. However the existing join code no longer works. I specify a Join in the map but it never appears in the SQL.
I have a class hierarchy mapped which includes a join (see below). The application uses some legacy tables that cannot be changed. Alternatively I could rewrite the code so that the joined properties can be mapped with a reference. However this is not really valid as it produces a domain object that does not exist.
public class IndividualMap : SubclassMap<Individual>
{
public IndividualMap()
{
Map(x => x.Title);
.....
Join("UserDefFields", x =>
{
x.KeyColumn("id");
x.Map(y => y.MembershipEnquirySource, "Anl9");
....
}
);

One table two model

i have posts table
id
type enum(A,Q)
title
content
and i need controllers questions_controller and answer_controller to use
please lead the right way to
1-
make 2 models (question and answer) and use 'posts' as model's table
2-
use post model in questions_controller and answer_controller
For me, it should be one table has one model
So better use
$uses = array('Post'); in your controllers.
Update:
As far I understood you want to filter the data depending from the type column. Actually your db model is wrong, because except if they are totally unrelated your answers need to belong to the question. This mean you need an extra column parent_id which will determine what is the parent node (question) and child nodes (answers).
If we have this assumption, then you can set a relation in the model like:
class Post extends AppModel {
...
var $belongsTo = array(
'Parent' => array('className' => 'Post','foreignKey' => 'parent_id', 'conditions' => 'Parent.parent_id IS NOT NULL')
);
...
}
So you automatically have Post and Post->Parent. But I think that it would be better to have 2 tables - one which holds the questions and another the answers.