relationship of a relationship in scope - Yii - yii

how can i get data from one table to another table's table's relations?
I have three tables: order, product, store. Within defaultscope, I have store_id in order model but that won't work because store_id is not in order table. But product_id is in order table which then can relate to store_id. The goal is obviously to filter out the store's orders.
Relations in Order:
'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
Relations in Product:
'orderProducts' => array(self::HAS_MANY, 'OrderProduct', 'product_id'),
'store' => array(self::BELONGS_TO, 'Store', 'store_id'),
i'm quite new to everything, I'm trying to understand this. I saw a post where they put scopes in the relations? Also, not understanding getTableAlias. Explanations (the what) would be appreciative rather than what i found a lot of what's wrong with the scope.
Sorry for the super newbie question.

Related

How to merge two tables into one DB schema

I'm new to database logic. I have Owners and Borrowers tables for a rental system. Both of them share similar fields like "email, username, password, etc." and also they differ from each other. My question is: How can I merge these similar fields into one table, because I'm pretty sure that my implementation is wrong. Another problem is that I can't properly determine relationship between Items and Item_statuses
When you design your tables, you don't organize them according to the similarities among the titles of the columns, but according to the relations between them.
Your design is working, you may just remove the 'item_statuses' table and create a 'status' column in the 'items' table.
But, let's do some brainstorming to improve the design and consider more of the business logic. We should ask some questions like:
How frequent will the owner be a borrower?
Should I keep track of all the statuses of the item?
If it is frequent for a person to be a borrower and an owner, we will end with duplicated records in our database, so we will make only one table called 'users' with the required fields, and refer the foreign keys in the 'items' table for the users.
If you should keep track of all the statuses of the item, create a separate table called 'items_log' with fields like 'Date' and with a foreign key that references to the item in the items table.
I would also keep track of all the borrowers of the item, so, I can make a 'item_borrowers' table with 'borrower' that references to the user, and 'item' that references to the item with fields like 'Date'.
Also keep in mind that you should store hashed password in the database not plain-text.

should i use BELONGS_TO, HAS_ONE o both in a self related table?

I have a table 'person' with a self relationships one to many, 'mother' and 'father', whom of course are persons. So 'person' table has two optional fields, 'mother_id' and 'father_id' pointing to other rows in the same table.
should i use BELONGS_TO, HAS_ONE o both?
Is important to remark that mother and father are optionals, in terms of the system an orphan is a valid entity, and of course a mother or father has no specific distintion other than the existent persons that point to him/her as father/mother.
Its also important to mention that for the sistem is irrelevant to know someone's childs, it's only relevant know the parents of a given person.
You can use a combination like this:
return array(
'father' => array(self::BELONGS_TO, 'Person', 'father_id'),
'mother' => array(self::BELONGS_TO, 'Person', 'mother_id'),
'mothersChildren' => array(self::HAS_MANY, 'Person', 'mother_id'),
'fathersChildren' => array(self::HAS_MANY, 'Person', 'father_id'),
);
So you want a HAS_MANY, not a HAS_ONE for your children, as each person may have more than one child.
You also see, that the problematic relation is the children relation: You'd have to use a JOIN like LEFT JOIN person c ON t.mother_id=c.id OR t.father_id=c.id which you can't define with Yii's relations. So you can define two relations and write a getter like getChildren() to create the union over mothersChildren and fathersChildren relations.
BELONGS_TO is usually used in reverse relation(child=>parent) while HAS_ONE is used in forward-relation (parent=>child). Therefore you best bet is , in my suggestion HAS_ONE.
The other important concept in this case is the concept of Weak Entity. Weak Entity does not exists without the parent or strong entity. Whenever you are referring back from Weak Entity to Strong Entity always use BELONGS_TO and HAS_ONE in other case.
Example of weak entity:UserProfile
User HAS_ONE UserProfile
A UserProfile BELONGS_TO A User.
Rememeber UserProfile does not exist without the User record, hence its weak entity.
In you case I an not sure when you say one to many, 'mother' and 'father'
However some possible relationships could be:
Person:Mother::1:1
Person:Father::1:1
Person:Children::1:M
If you are talking about the 3rd case, the relationship will be BELONGS_TO

Searching based on many-to-many relationship

I have a many-to-many relationship for objects in an SQL database in a Dancer server, and I need to be able to search objects based on a criteria on the other. In other words, I need to be able to do what is asked in this question but in Dancer.
The relationship is modeled as described in the Dancer DBIx::Class documentation here.
I see examples of how to search based on one-to-many relationships here but I have not been able to translate this to many-to-many.
If you read the DBIx::Class docs carefully, you'll see that many-to-many is not a relationship but a relationship bridge.
You can still filter on related columns by joining the relationships that form the many-to-many:
my $rs = $schema->resultset('Artist')->search({
'tracks.name' => 'Always',
},{
join => { cds => 'tracks' },
}
);

Rails Model class relations in ternary many-many case

I have a 'users' table, 'groups' table and 'invitations' table(join table). I am trying to build a relation between 'invitors'(class_name 'User'), 'invitees'(class_name 'User') and 'groups'(class_name 'Group') where 'invitations'(class_name 'Invitation') is the join table with foreign keys 'invitor_id', 'invitee_id' and 'group_id'.
(Many 'Invitors' can give 'Invitations' to Many 'Invitees' to join Many 'Groups')
I tried several ways by explicitly specifying :foreign_key and :class_name in my Model classes, but in vain. I have just started learning the 'activerecord relations' concepts in rails, and i really want to make efficient use of it. Can someone help me out with this problem.
The problem you are trying to solve here, in ActiveRecord terms, is a "self referential" association. Ryan Bates provides a great example that answers your situation almost exactly:
http://railscasts.com/episodes/163-self-referential-association
In his example you can replace "Friendship" with your idea of an "Invitation". You will need to add a group_id to his Friendship model to keep track of which Group the Invitation is related to.

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model?
Product
=======
product_id
....
Cart
====
cart_id
....
Carts_Products
==============
cart_id
product_id
quantity
Since you are storing data in your join table (for a HABTM relation), you're situation looks quite similar to Rail's 'through' relation (seen at the bottom of this diagram). As such, you will want to create a model for that table and use Cake's 'with' relation setting seen on the HABTM page of the book. You should then be able to access the data stored on the join table. By convention...
your tables should be named products, carts, carts_products
your models should be named Product, Cart, CartsProduct
I would also add that (for CakePHP2) the column names for product and cart need to change.
products
========
id
name
...
carts
=====
id
create_date
...
I believe the answer to your question will be revealed on careful reading of this documentation page.