Zend_Db_Table join instance details in Zend Framework tutorial, zendzend_db_table

Source: Internet
Author: User

Zend_Db_Table join instance details in Zend Framework tutorial, zendzend_db_table

This example describes how to associate a Zend_Db_Table table in Zend Framework. We will share this with you for your reference. The details are as follows:

Introduction:

In RDBMS, tables have various relationships, such as multi-correspondence and multi-correspondence.

The Zend Framework provides some methods to help us implement these relationships.

Define link:

The following is the relational definition of the example used in this article:

<?phpclass Accounts extends Zend_Db_Table_Abstract{  protected $_name      = 'accounts';  protected $_dependentTables = array('Bugs');}classclass  protected  protectedclass  protected}Products extends Zend_Db_Table_Abstract{  protected $_name      = 'products';  protected $_dependentTables = array('BugsProducts');}Bugs extends Zend_Db_Table_Abstract{  protected $_name      = 'bugs';$_dependentTables = array('BugsProducts');$_referenceMap  = array(    'Reporter' => array(      'columns'      => 'reported_by',      'refTableClass'   => 'Accounts',      'refColumns'    => 'account_name'    ),    'Engineer' => array(      'columns'      => 'assigned_to',      'refTableClass'   => 'Accounts',      'refColumns'    => 'account_name'    ),    'Verifier' => array(      'columns'      => array('verified_by'),      'refTableClass'   => 'Accounts',      'refColumns'    => array('account_name')    )  );}BugsProducts extends Zend_Db_Table_Abstract{  protected $_name = 'bugs_products';$_referenceMap  = array(    'Bug' => array(      'columns'      => array('bug_id'),      'refTableClass'   => 'Bugs',      'refColumns'    => array('bug_id')    ),    'Product' => array(      'columns'      => array('product_id'),      'refTableClass'   => 'Products',      'refColumns'    => array('product_id')    )  );

The example defines four classes: Accounts, Products, Bugs, and BugsProducts. Here, Accounts, Products, and Bugs are three entity tables, while BugsProducts are Relational Tables.

Let's analyze the three entities. One Account has multiple bugs, which are one-to-many relationships, while the Bug and Product are multiple-to-many relationships.

$ _ DependentTables is an object name associated with this object. Note that the object name should be written instead of the associated database name.

$ _ ReferenceMap array is used to define the relationship with other tables. Here, you can set the relationship with those tables. The first setting is the Rule Key, that is, the 'reporter 'and 'engineer' in the above example. The role of the Rule Key is actually the name of a link and does not need to be the same as that of other database tables or other object names. It's just for marking. Later, we can see the role of this Rule Key.

Each Rule has the following definitions)

Columns => set the field names associated with other tables. The above 'report _ by' is the report_by field of the table Bugs in the database. There is only one field. You can also set multiple fields.

RefTableClass => used to set a table that has a relationship with this table. Note that the object name of the target table must be used instead of the table name. In this example, it is associated with the 'account' object.

RefColumns => set the fields of the table in which the contact occurs. You can write multiple columns. If multiple fields are in contact with each other, it corresponds to columns. This setting is optional. If it is null, the joined field is automatically set as the primary key of the joined table. In the above example, the primary key is not used as the associated field, so it is set manually.

OnDelete => optional field, set the action when deleting.
OnUpdate => optional field, set the action when updating the table.

The preceding relationships are defined.

Retrieve data from the associated table:

If we have obtained a query result, we can use the following statement to obtain the query result of the table associated with the result:

$row->findDependentRowset($table, [$rule]);

This method generally uses two entity tables that correspond to multiple objects. How can we retrieve data from one entity table from another entity table in two entity tables and one relational table corresponding to multiple objects, we will describe it below.

The first field $ table is the class name of the table associated with the table. The second field is optional. It is the rule key we just mentioned. It is the name of the link. If it is omitted, it is the first link in the table by default. The following is an example:

<?php$accountsTable   = new Accounts();$accountsRowset   = $accountsTable->find(1234);$user1234      = $accountsRowset->current();$bugsReportedByUser = $user1234->findDependentRowset('Bugs');

In this example, we first read a user numbered 1234 and then look for the bug reported by this guy, because zend is the first association by default, therefore, the first associated with the Account is 'reporter, so the Reporter record is retrieved.

If we want to retrieve other records, such as Engineer, we can follow the methods below:

<?php$accountsTable   = new Accounts();$accountsRowset   = $accountsTable->find(1234);$user1234      = $accountsRowset->current();$bugsAssignedToUser = $user1234->findDependentRowset('Bugs', 'Engineer');

In addition to using findDependentRowset, we can also use a mechanism called "Magic Method. This is because it seems to be magic. Therefore, the findDependentRowset ('<TableClass>', '<Rule>') method can be equivalent to the following:

-$ Row-> find <TableClass> ()
-$ Row-> find <TableClass> By <Rule> ()

Note: This mechanism was first seen in Ruby on Rails. <TableClass> and <Rule> must use the same name as the associated class name and the associated Rule Key to take effect. The following is an example:

<?php$accountsTable  = new Accounts();$accountsRowset  = $accountsTable->find(1234);$user1234     = $accountsRowset->current();// Use the default reference rule$bugsReportedBy  = $user1234->findBugs();// Specify the reference rule$bugsAssignedTo  = $user1234->findBugsByEngineer();
<?php$bugsTable     = new Bugs();$bugsRowset    = $bugsTable->fetchAll('bug_status = ?', 'NEW');$bug1       = $bugsRowset->current();// Use the default reference rule$reporter     = $bug1->findParentAccounts();// Specify the reference rule$engineer     = $bug1->findParentAccountsByEngineer();

Obtain fields from the parent table:

We have just introduced the method of getting from one to more in a multi-relationship. Now, in turn, getting one from multiple is actually the record corresponding to one from multiple.

Similar statements:

$row->findParentRow($table, [$rule]);

Similarly, $ table is the class name, and the optional parameter $ rule is filled with the corresponding Rule Key. The following is an example:

<?php$bugsTable     = new Bugs();$bugsRowset    = $bugsTable->fetchAll(array('bug_status = ?' => 'NEW'));$bug1       = $bugsRowset->current();$reporter     = $bug1->findParentRow('Accounts');

Unlike the above, the returned result is a set of multiple records, and this return must be a record. The following example sets Rule:

<?php$bugsTable     = new Bugs();$bugsRowset    = $bugsTable->fetchAll('bug_status = ?', 'NEW');$bug1       = $bugsRowset->current();$engineer     = $bug1->findParentRow('Accounts', 'Engineer');

Just fill in Rule. Similarly, this method also has a "magic field ". FindParentRow ('<TableClass>', '<Rule>') corresponds:

-$ Row-> findParent <TableClass> ()
-$ Row-> findParent <TableClass> By <Rule> ()

Example:

Obtain the fields of the many-to-many relationship table:

The above two methods describe one-to-many usage. below is the many-to-many usage. We use the following method to obtain data from multiple to multiple Relational Tables:

$row->findManyToManyRowset($table, $intersectionTable, [$rule1, [$rule2]]);

Here the parameter is changed to four because a relational table needs to be added to store many-to-many relationships.

$ Table is the class name of the table with many-to-many relationships, and $ intersectionTable is the class name of the relational table with intermediate storage relationships. $ Rule1 and $ rule2 are the Rule keys of the preceding two data tables. An example of omitting the Rule Key is as follows:

<?php$bugsTable    = new Bugs();$bugsRowset    = $bugsTable->find(1234);$bug1234     = $bugsRowset->current();$productsRowset  = $bug1234->findManyToManyRowset('Products', 'BugsProducts');

The following is an example of calling all the parameters of the method:

<?php$bugsTable    = new Bugs();$bugsRowset    = $bugsTable->find(1234);$bug1234     = $bugsRowset->current();$productsRowset  = $bug1234->findManyToManyRowset('Products', 'BugsProducts', 'Bug');

The "Magic method" is corresponding to findManyToManyRowset ('<TableClass>', '<IntersectionTableClass>', '<Rule1>', '<Rule2> ')
-$ Row-> find <TableClass> Via <IntersectionTableClass> ()
-$ Row-> find <TableClass> Via <IntersectionTableClass> By <Rule1> ()
-$ Row-> find <TableClass> Via <IntersectionTableClass> By <Rule1> And <Rule2> ()

Example:

<?php$bugsTable    = new Bugs();$bugsRowset    = $bugsTable->find(1234);$bug1234     = $bugsRowset->current();// Use the default reference rule$products     = $bug1234->findProductsViaBugsProducts();// Specify the reference rule$products     = $bug1234->findProductsViaBugsProductsByBug();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.