


Detailed explanation of Zend_Db_Table table association examples in Zend Framework tutorial
Jan 05, 2017 am 09:40 AMThe example in this article describes the usage of Zend_Db_Table table association in Zend Framework. Share it with everyone for your reference, the details are as follows:
Introduction:
In RDBMS, there are various relationships between tables, one-to-many correspondence, many-to-many correspondence, etc.
The Zend framework provides some methods to facilitate us to realize these relationships.
Definition of relationship:
The following is the relationship definition of the example used in this article:
<?php class Accounts extends Zend_Db_Table_Abstract { protected $_name = 'accounts'; protected $_dependentTables = array('Bugs'); } class class protected protected class 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') ) );
We see that four classes are defined in the example: Accounts, Products, Bugs, BugsProducts . Among them, Accounts, Products and Bugs are three entity tables, and BugsProducts is a relationship table.
Let’s analyze these three entities again. An Account has multiple Bugs. There is a one-to-many relationship between them, while Bug and Product have a many-to-many relationship.
$_dependentTables is an object name associated with the object. Note here that you should write the object name 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 and what kind of relationship there is. The first thing to set is the Rule Key, which is the 'Reporter', 'Engineer' and the like in the above example. The function of Rule Key is actually the name of a relationship, and it does not need to be the same as the name of other database table names or other object names. Just for marking, we can see the role of this Rule Key later.
There are some definitions below each Rule: (No special instructions, all are explained with the 'Reporter' relationship as above)
columns=> Set fields associated with other tables name, the 'report_by' above is the report_by field of the table Bugs in the database. There is only one field here, but multiple fields can also be set.
refTableClass=>Used to set the table that is related to this table. Note here that you must use the name of the object of the target table instead of the table name. In the example, it is associated with the 'Account' object.
refColumns =>Set the fields of the table where the contact occurs. You can write more than one. If it is related to multiple fields, it should correspond to columns. This setting is actually optional. If it is empty, the related field is automatically set as the primary key of the related table. In the above example, the primary key is not used as the related 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 above defines the relationship.
Get data from the associated table:
If we have already obtained a query result, we can use the following statement to obtain the query result of the table associated with this result:
$row->findDependentRowset($table, [$rule]);
This method is generally used in the two entity tables corresponding to Duoduo. In the two entity tables and a relationship table corresponding to Duoduo, how to retrieve data from one entity table and another entity table will be described below.
The first field $table refers to the class name corresponding to the table that this table is associated with. The second field is optional and is the rule key we just mentioned, which is the name of the relationship. If omitted, it defaults to the first relationship in the table. The following is an example:
<?php $accountsTable = new Accounts(); $accountsRowset = $accountsTable->find(1234); $user1234 = $accountsRowset->current(); $bugsReportedByUser = $user1234->findDependentRowset('Bugs');
In the example, we first read a user numbered 1234, and then found out what bug this guy reported. Since zend is the first association by default, it occurs with Account. The first one associated is 'Reporter, so the Reporter record is taken out.
If we want to take out other records, such as Engineer, we can follow the following method:
<?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 something called "Magic Method" mechanism. The reason why it is called so is because it seems to be a magic trick. So the method findDependentRowset('
- $row->find
- $row- >find
Note: This mechanism was first seen in Ruby on Rails. The
<?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 just introduced the method of getting from one to many in a one-many relationship. Now we turn around and get one from many. In fact, It is to take the corresponding record from one of many.
Similarly, we have this statement:
$row->findParentRow($table, [$rule]);
Similarly, $table is the class name, and the optional parameter $rule is filled in 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');
The difference from the above is that what is returned above is a collection of multiple records, and what is returned this time must be one record. The following example is to set the Rule:
<?php $bugsTable = new Bugs(); $bugsRowset = $bugsTable->fetchAll('bug_status = ?', 'NEW'); $bug1 = $bugsRowset->current(); $engineer = $bug1->findParentRow('Accounts', 'Engineer');
Just fill in the Rule. Similarly, this method also has "magic fields". findParentRow('
- $row->findParent
- $row->findParent
Example:
Get the fields of the many-to-many relationship table:
上面兩個方法講述了一對多的使用,下面就是多對多了。我們使用如下方法取得多對多關(guān)系表的數(shù)據(jù):
$row->findManyToManyRowset($table, $intersectionTable, [$rule1, [$rule2]]);
這里參數(shù)變成了4個,因為需要增加一個關(guān)系表來存儲多對多的關(guān)系。
$table是與之發(fā)生多對多關(guān)系的表的類名,$intersectionTable是中間存儲關(guān)系的關(guān)系表的類名。$rule1和$rule2是上面兩個數(shù)據(jù)表的Rule Key。省略Rule Key的例子如下:
<?php $bugsTable = new Bugs(); $bugsRowset = $bugsTable->find(1234); $bug1234 = $bugsRowset->current(); $productsRowset = $bug1234->findManyToManyRowset('Products', 'BugsProducts');
下面是該方法的全部參數(shù)調(diào)用例子:
<?php $bugsTable = new Bugs(); $bugsRowset = $bugsTable->find(1234); $bug1234 = $bugsRowset->current(); $productsRowset = $bug1234->findManyToManyRowset('Products', 'BugsProducts', 'Bug');
這次的“魔術(shù)方法”是,對應(yīng) findManyToManyRowset('
- $row->find
- $row->find
- $row->find
例子:
<?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();
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計有所幫助。
更多Zend Framework教程之Zend_Db_Table表關(guān)聯(lián)實例詳解相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
