We've learned how to use Active record (AR) to get data from a single datasheet. In this section, we explain how to use AR to connect multiple related data tables and retrieve the associated (join) data sets.
To use relational AR, we recommend that you define a primary key-foreign KEY constraint in the table that you want to associate. These constraints can help ensure the consistency and integrity of the relevant data.
This example describes how multiple relational tables use active record by modifying the Yii Framework Development Tutorial (25) Database-query Builder example.
Before we use AR to execute the associated query, we need to let ar know how an AR class is associated to another.
The relationship between the two AR classes is directly related to the relationship between the data tables represented by the AR class. From a database perspective, there are three relationships between tables A and B: One-to-many (One-to-many, such as Tbl_user and Tbl_post), one-to-one (one-to-one such as Tbl_user and Tbl_profile), and Many-to-many (many-to -many such as Tbl_category and Tbl_post). In AR, there are four kinds of relationships:
Belongs_to (belongs to): If the relationship between Table A and B is one-to-many, table B belongs to table A (for example, Post belongs to User);
Has_many (multiple): If the relationship between table A and B is One-to-many, then A has multiple B (for example, User has multiple Post);
Has_one (one): This is a special case of Has_many, A has a maximum of B (for example, User has a profile at most);
Many_many: This corresponds to a many-to-many relationship in the database. Since most DBMS does not directly support many-to-many relationships, it is necessary to have a relational table that divides the many-to-many relationship into one-to-many relationships. In our sample data structure, Tbl_post_category is used for this purpose. In AR terminology, we can explain many_many as a combination of belongs_to and has_many. For example, a post belongs to more than one (belongs to many) Category and Category has multiple (has many) post.
The definition of relationships in AR requires overwriting the relations () method in Cactiverecord. This method returns a relational configuration array. Each array element represents a single relationship through the following format.
In Query Builder we use the following SQL query statement
SELECT C.firstname, C.lastname, c.address,c.email from
customer C
INNER JOIN
employee E on
c. Supportrepid=e.employeeid
WHERE e.employeeid=4
involves two forms the relationship between employee and Customer,employee and customer is one-to-many, which means that an employee can be responsible for multiple customers. Employee to customer relationship for Has_many, customer to employee relationship for Has_one. So you can define employee and customer as follows:
Customer.php
class Customer extends Cactiverecord
{public
static function model ($className =__class__)
{return
Parent::model ($className);
}
Public Function tablename ()
{return
' Customer ';
}
}
Employee.php
class Employee extends Cactiverecord
{public
static function model ($className =__class (__)
{return
Parent::model ($className);
}
Public Function tablename ()
{return
' Employee ';
}
Public Function relations ()
{return
array (
' customers ' =>array (self::has_many, ' Customer ', ' Supportrepid '),
);
}
Because this example only uses the customer corresponding to the employee query, the relations method is defined for the class only. The corresponding table and foreign keys are customer and supportrepid.
Then modify the Sitecontroller Indexaction method:
Public Function Actionindex ()
{
$employee =employee::model ()->FINDBYPK (4);
$this->render (' index ', array (
' model ' => $employee->customers,
));
}