多关系的CActiveRecord使用举例
相关的数据库代码
- # --------------------------------------------------------
- # Host: 10.0.2.2
- # Server version: 5.1.58-1ubuntu1
- # Server OS: debian-linux-gnu
- # HeidiSQL version: 6.0.0.3603
- # Date/time: 2011-11-30 19:33:04
- # --------------------------------------------------------
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
- /*!40101 SET NAMES utf8 */;
- /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
- /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
- # Dumping structure for table testdrive.tbl_profile
- CREATE TABLE IF NOT EXISTS `tbl_profile` (
- `owner_id` int(11) NOT NULL DEFAULT '0',
- `nickname` varchar(50) DEFAULT NULL,
- `birthday` date DEFAULT NULL,
- `regdate` timestamp NULL DEFAULT NULL,
- PRIMARY KEY (`owner_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- # Dumping data for table testdrive.tbl_profile: 0 rows
- /*!40000 ALTER TABLE `tbl_profile` DISABLE KEYS */;
- INSERT INTO `tbl_profile` (`owner_id`, `nickname`, `birthday`, `regdate`) VALUES
- (1, '1', '0000-00-00', '2011-11-30 14:32:01'),
- (2, '2', '2011-11-21', '2011-11-30 14:32:01'),
- (3, '3', '0000-00-00', '2011-11-30 14:32:01'),
- (4, '3', '0000-00-00', '2011-11-30 14:32:01');
- /*!40000 ALTER TABLE `tbl_profile` ENABLE KEYS */;
- # Dumping structure for table testdrive.tbl_user
- CREATE TABLE IF NOT EXISTS `tbl_user` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `username` varchar(128) CHARACTER SET latin1 NOT NULL,
- `password` varchar(128) CHARACTER SET latin1 NOT NULL,
- `email` varchar(128) CHARACTER SET latin1 DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
- # Dumping data for table testdrive.tbl_user: 4 rows
- /*!40000 ALTER TABLE `tbl_user` DISABLE KEYS */;
- INSERT INTO `tbl_user` (`id`, `username`, `password`, `email`) VALUES
- (1, '1', '1', '1'),
- (2, '2', '3', '3333456'),
- (3, '2', '33', '33334'),
- (4, 'admin', '123456', 'admin@admin.com');
- /*!40000 ALTER TABLE `tbl_user` ENABLE KEYS */;
# -------------------------------------------------------- # Host: 10.0.2.2 # Server version: 5.1.58-1ubuntu1 # Server OS: debian-linux-gnu # HeidiSQL version: 6.0.0.3603 # Date/time: 2011-11-30 19:33:04 # -------------------------------------------------------- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET NAMES utf8 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; # Dumping structure for table testdrive.tbl_profile CREATE TABLE IF NOT EXISTS `tbl_profile` ( `owner_id` int(11) NOT NULL DEFAULT '0', `nickname` varchar(50) DEFAULT NULL, `birthday` date DEFAULT NULL, `regdate` timestamp NULL DEFAULT NULL, PRIMARY KEY (`owner_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; # Dumping data for table testdrive.tbl_profile: 0 rows /*!40000 ALTER TABLE `tbl_profile` DISABLE KEYS */; INSERT INTO `tbl_profile` (`owner_id`, `nickname`, `birthday`, `regdate`) VALUES (1, '1', '0000-00-00', '2011-11-30 14:32:01'), (2, '2', '2011-11-21', '2011-11-30 14:32:01'), (3, '3', '0000-00-00', '2011-11-30 14:32:01'), (4, '3', '0000-00-00', '2011-11-30 14:32:01'); /*!40000 ALTER TABLE `tbl_profile` ENABLE KEYS */; # Dumping structure for table testdrive.tbl_user CREATE TABLE IF NOT EXISTS `tbl_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(128) CHARACTER SET latin1 NOT NULL, `password` varchar(128) CHARACTER SET latin1 NOT NULL, `email` varchar(128) CHARACTER SET latin1 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; # Dumping data for table testdrive.tbl_user: 4 rows /*!40000 ALTER TABLE `tbl_user` DISABLE KEYS */; INSERT INTO `tbl_user` (`id`, `username`, `password`, `email`) VALUES (1, '1', '1', '1'), (2, '2', '3', '3333456'), (3, '2', '33', '33334'), (4, 'admin', '123456', 'admin@admin.com'); /*!40000 ALTER TABLE `tbl_user` ENABLE KEYS */;
/yii_dev/testwebap/protected/models/TblUser.php
- <?php
- class TblUser extends CActiveRecord
- {
- public static function model($className=__CLASS__)
- {
- return parent::model($className);
- }
- public function tableName()
- {
- return 'tbl_user';
- }
- public function relations ()
- {
- return array(
- /*关系名字*/
- 'profile' => array(
- /*关联方式*/
- self::HAS_ONE,
- /*关系表的CActiveRecord类*/
- 'TblProfile',
- /*关系表的外键,即tbl_profile通过哪一个字段和tbl_user主键关联.*/
- 'owner_id',
- /*select显示的列。用string:'regdate,nickname',或者数组array('regdate','nickname')*/
- 'select' => array('regdate','nickname'),
- /*条件:where 子句 。要保证实例化使用使用对象访问是能够访问,否这将会报non-boject错误*/
- 'condition' => 'owner_id<=4',
- /*order by 排序*/
- 'order' => 'owner_id DESC , nickname ASC',
- /*where条件中追加 以AND 开头的条件子句。他会智能的判断是否该加AND。这样省去了你组合where条件。不会和'condition'选项冲突
- WHERE (owner_id<=4) AND ( nickname!='abc') AND (`profile`.`owner_id`=:ypl0) ORDER BY owner_id DESC , nickname ASC
- */
- 'on' => " nickname!='abc' ",
- ),
- );
- }
- }
<?php class TblUser extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'tbl_user'; } public function relations () { return array( /*关系名字*/ 'profile' => array( /*关联方式*/ self::HAS_ONE, /*关系表的CActiveRecord类*/ 'TblProfile', /*关系表的外键,即tbl_profile通过哪一个字段和tbl_user主键关联.*/ 'owner_id', /*select显示的列。用string:'regdate,nickname',或者数组array('regdate','nickname')*/ 'select' => array('regdate','nickname'), /*条件:where 子句 。要保证实例化使用使用对象访问是能够访问,否这将会报non-boject错误*/ 'condition' => 'owner_id<=4', /*order by 排序*/ 'order' => 'owner_id DESC , nickname ASC', /*where条件中追加 以AND 开头的条件子句。他会智能的判断是否该加AND。这样省去了你组合where条件。不会和'condition'选项冲突 WHERE (owner_id<=4) AND ( nickname!='abc') AND (`profile`.`owner_id`=:ypl0) ORDER BY owner_id DESC , nickname ASC */ 'on' => " nickname!='abc' ", ), ); } }
/yii_dev/testwebap/protected/models/TblProfile.
- <?php
- class TblProfile extends CActiveRecord
- {
- public static function model ($className = __CLASS__)
- {
- return parent::model($className);
- }
- public function tableName ()
- {
- return 'tbl_profile';
- }
- }
<?php class TblProfile extends CActiveRecord { public static function model ($className = __CLASS__) { return parent::model($className); } public function tableName () { return 'tbl_profile'; } }
/yii_dev/testwebap/protected/modules/testmod/controllers/DefaultController.php
- <?php
- class DefaultController extends Controller
- {
- public function actionIndex ()
- {
- $this->render('index');
- }
- public function actionIndex2 ()
- {
- $user = TblUser::model()->findByPk(2);
- var_dump( '\nusername:' . $user->username . 'nickname:' . $user->profile->nickname);
- //or
- $userModel = new TblUser();
- $user = $userModel->findByPk(2);
- var_dump( '\nusername:' . $user->username . 'nickname:' . $user->profile->nickname);
- $users = TblUser::model()->findAll();
- foreach ($users as $user) {
- var_dump( '\nid:' . $user->id . 'username:' . $user->username . 'nickname:' . $user->profile->nickname);
- }
- $users = TblUser::model()->findAll();
- exit();
- }