Zend Framework Tutorial Zend_db_table Table Association example, zendzend_db_table_php Tutorial

Source: Internet
Author: User
Tags zend framework ruby on rails


Zend Framework Tutorial Zend_db_table Table Association example, zendzend_db_table




This example describes the Zend_db_table Table association usage in the Zend Framework. Share to everyone for your reference, as follows:



Introduced:



In RDBMS, there are various relationships between tables, with a lot of correspondence, a lot of correspondence, and so on.



The Zend Framework provides some ways to facilitate our implementation of these relationships.



Define relationships:



The following is a relationship definition for 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 = ' Produc  TS '; Protected $_dependenttables = Array (' bugsproducts ');} Bugs extends zend_db_table_abstract{protected $_name = ' Bugs '; $_dependenttables = Array (' bugsproducts '); $_reference       Map = Array (' Reporter ' = = Array (' columns ' = ' reported_by ', ' reftableclass ' = ' Accounts ',      ' Refcolumns ' = ' account_name '), ' Engineer ' = = Array (' columns ' = ' assigned_to ', ' Reftableclass ' = ' Accounts ', ' refcolumns ' = ' account_name '), ' Verifier ' = = Array (' Colum NS ' = = Array (' verified_by '), ' reftableclass ' = ' Accounts ', ' refcolumns ' = = Array (' Account_na Me '));} Bugsproducts extends zend_db_table_abstract{protected $_name = ' bugs_products '; $_referencemap = Array (' Bug ' = = Array (' columns ' = = Array (' bug_id '), ' re      Ftableclass ' + ' Bugs ', ' refcolumns ' = = Array (' bug_id '), ' Product ' = = Array (' Columns '  = = Array (' product_id '), ' reftableclass ' = ' products ', ' refcolumns ' = = Array (' product_id ')) );


We see an example of four classes defined: Accounts,products,bugs,bugsproducts. Where accounts,products and bugs are three entity tables, and bugsproducts is a relational table.



Let's analyze the three entities, one account has multiple bugs, they are a one-to-many relationship, and the bug and product are many-to-many relationships.



$_dependenttables is an object name associated with the object, which is noted here to write the object name instead of the associated database name.



The $_referencemap array is used to define relationships with other tables, where it is possible to set relationships with those tables. The first set is Rule Key, which is the ' Reporter ' of the above example, ' Engineer ' and the like. Rule key is actually the name of a relationship and does not need to be the same name as other database table names or other object names. Just for the sake of tagging, at the back, we can see the role of this rule key.



Each rule has the following definitions: (no special instructions are described as above ' Reporter ' relationship)



Columns=> sets the field names associated with other tables, such as ' report_by ' on the Report_by field of the table bugs in the database. There is only one field, and you can set multiple fields.



The reftableclass=> is used to set the table that has a relationship with this table. It is important to note that the object name of the target table must be used instead of the name of the table, and the example is associated with the ' account ' object.



Refcolumns + = Sets the field of the table to which the contact occurs. Can write multiple, if and more than one field to contact, here to and columns correspondence. This setting is optional and, if empty, the associated field is automatically set as the primary key for the associated table. The above example does not use the primary key as the associated field, so it is set manually.



Ondelete=> optional field, set when Delete is action.
onupdate=> an optional field that sets the action when the table is updated.



Define the relationship above.



To fetch data from the associated table:



If we have already obtained a query result, we can get the result of the table associated with the results by a statement:


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


This method is generally used with a number of corresponding two entity tables, in the lot corresponding to the two entity table and a relational table how to remove the data from one entity table of another entity table, we will describe below.



The first field, $table, refers to the class name corresponding to the table to which the table wants to relate. The second field is optional, the rule key we just mentioned, the name of the relationship, or, if omitted, the first relationship in the table. Here is an example:


<?php$accountstable   = new Accounts (); $accountsRowset   = $accountsTable->find (1234); $user 1234      = $ Accountsrowset->current (); $bugsReportedByUser = $user 1234->finddependentrowset (' Bugs ');


example, we first read a user number 1234, and then to find out what this guy reported a bug, because Zend default is the first association, so here and account associated with the first is ' Reporter, so we took out the Reporter record.



If we want to take out other records, such as engineer, we can follow the following methods:


<?php$accountstable   = new Accounts (); $accountsRowset   = $accountsTable->find (1234); $user 1234      = $ Accountsrowset->current (); $bugsAssignedToUser = $user 1234->finddependentrowset (' Bugs ', ' Engineer ');


In addition to using Finddependentrowset, we can also use a mechanism called magic method. That's what it's called because it seems to be magic. So the method Finddependentrowset (",") can be equivalent to the following:



-$row->find()
-$row->findby()



Note: This mechanism is what we saw in Ruby on Rails at the very beginning. It isimportant to use exactly the same name as the associated class name and the associated name (Rule Key) to take effect. Here is an example:


<?php$accountstable  = new Accounts (); $accountsRowset  = $accountsTable->find (1234); $user 1234     = $ Accountsrowset->current ();//Use the default reference Rule$bugsreportedby  = $user 1234->findbugs ();// Specify the reference rule$bugsassignedto  = $user 1234->findbugsbyengineer ();
<?php$bugstable     = new Bugs (); $bugsRowset    = $bugsTable->fetchall (' Bug_status =? ', ' new '); $bug 1       = $ Bugsrowset->current ();//Use the default reference rule$reporter     = $bug 1->findparentaccounts ();//Specify The reference Rule$engineer     = $bug 1->findparentaccountsbyengineer ();


Get fields from parent table:



We have just introduced a lot of relationships in the way from one to many, now we in turn, from more than one, in fact, from a more than one to take his corresponding record.



Similarly, we have this statement:


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


Similarly, the $table is the class name, and the optional parameter $rule fills in the corresponding rule Key. Here is an example:


<?php$bugstable     = new Bugs (); $bugsRowset    = $bugsTable->fetchall (Array (' Bug_status =? ' = ' new '); $ Bug1       = $bugsRowset->current () $reporter     = $bug 1->findparentrow (' Accounts ');


Unlike above, the return is a collection of multiple records, and this time the return is necessarily a record. The following example sets the rule:


<?php$bugstable     = new Bugs (); $bugsRowset    = $bugsTable->fetchall (' Bug_status =? ', ' new '); $bug 1       = $ Bugsrowset->current (); $engineer     = $bug 1->findparentrow (' Accounts ', ' engineer ');


All you need is the rule to fill in. Similarly, this method also has a "magic field". Findparentrow (', ') corresponds to:



-$row->findparent()
-$row->findparentby()



Example:



To obtain a field for a many-to-many relationship table:




$row->findmanytomanyrowset ($table, $intersectionTable, [$rule 1, [$rule 2]]);


Here the parameter becomes 4 because you need to add a relational table to store many-to-many relationships.



$table is the class name of the table with many-to-many relationships, $intersectionTable is the class name of the relational table for the intermediate storage relationship. $rule 1 and $rule2 are the rule keys for the two data tables above. An example of omitting rule key is as follows:


<?php$bugstable    = new Bugs (); $bugsRowset    = $bugsTable->find (1234); $bug 1234     = $bugsRowset Current (); $productsRowset  = $bug 1234->findmanytomanyrowset (' products ', ' bugsproducts ');


The following is an example of all the parameter invocation of the method:


<?php$bugstable    = new Bugs (); $bugsRowset    = $bugsTable->find (1234); $bug 1234     = $bugsRowset Current (); $productsRowset  = $bug 1234->findmanytomanyrowset (' products ', ' bugsproducts ', ' Bugs ');


This time the "Magic Method" is, corresponding to the Findmanytomanyrowset (",", ",")
-$row->findVia()
-$row->findViaby()
-$row->findVia by and()



Example:


<?php$bugstable    = new Bugs (); $bugsRowset    = $bugsTable->find (1234); $bug 1234     = $bugsRowset Current ();//Use the default reference rule$products     = $bug 1234->findproductsviabugsproducts ();//Specify the Reference rule$products     = $bug 1234->findproductsviabugsproductsbybug ();


More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "



It is hoped that this article will help you to design PHP based on the Zend Framework framework.



Articles you may be interested in:


    • Zend Framework Framework Tutorial Zend_db_table_rowset Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table_row Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table Usage
    • Zend Framework Tutorial Zend_form component implement form submission and display Error prompt method
    • Introduction to Zend Framework Development Classic Tutorial
    • Zend Framework Smarty Extension Implementation method
    • Code analysis of routing mechanism of Zend framework framework
    • Zend Framework implementation of basic functions of the message book (with demo source download)
    • The Zend framework implements the method of storing the session in Memcache
    • Zend Framework Paging Class usage
    • Zend Framework implements multi-file upload function instances
    • Zend Framework Introduction Environment configuration and the first Hello World example (with demo source download)
    • Zend Framework Tutorial to connect the database and perform additions and deletions of the method (attached to the demo source download)





http://www.bkjia.com/PHPjc/1113708.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113708.html techarticle Zend Framework Tutorial Zend_db_table Table Association instance, zendzend_db_table This example describes the Zend_db_table Table association usage in the Zend framework. Share to everyone for reference, with ...


  • 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.