1.data tables
1.1 pkrss_test data table
CREATE TABLE IF NOT EXISTS `pkrss_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` varchar(255) NOT NULL,
`v` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `k` (`k`)
) AUTO_INCREMENT=3 ;
INSERT INTO `pkrss_test` (`id`, `k`, `v`) VALUES
(1, 'k1', 'v1'),
(2, 'k2', 'v2');
1.2 pkrss_test2 data table
CREATE TABLE IF NOT EXISTS `pkrss_test2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` varchar(255) NOT NULL,
`v2` varchar(255) DEFAULT NULL,
`pid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`)
) AUTO_INCREMENT=3 ;
INSERT INTO `pkrss_test2` (`id`, `k`, `v2`, `pid`) VALUES
(1, 'k1', 'k1', 1),
(2, 'k2', 'v2', 2);
ALTER TABLE `pkrss_test2`
ADD CONSTRAINT `pkrss_test2_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `pkrss_test` (`id`);
2.php code and test result
2.1 TestModel.php
<?php
/*
CREATE TABLE IF NOT EXISTS `pkrss_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` varchar(255) NOT NULL,
`v` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `k` (`k`)
) AUTO_INCREMENT=3 ;
INSERT INTO `pkrss_test` (`id`, `k`, `v`) VALUES
(1, 'k1', 'v1'),
(2, 'k2', 'v2');
*/
class TestModel extends CActiveRecord {
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{test}}';
}
public $k='';
public $v='';
public function primaryKey()
{
return 'id';
}
public function relations()
{
return array(
'key'=>array(self::HAS_MANY, 'Test2Model', 'pid'),
);
}
public static function testFind(){
$model = self::model();
// return db object
$result = $model->find('k=:k',array(':k'=>'k1'));
// return data array
$result = $result->getAttributes();
// return null object
$result = $model->find('k=:k',array(':k'=>'none'));
if($result)
$result = $result->getAttributes();
}
public static function testFindAll(){
$model = self::model();
// return db object array
$result = $model->findAll('k=:k',array(':k'=>'k1'));
foreach($result as $row){
// return data array
$v = $row->getAttributes();
}
// return: array()
$result = $model->findAll('k=:k',array(':k'=>'null'));
// count($result) == 0
}
public static function testFindByPk(){
$model = self::model();
// return db object
$result = $model->findByPk(1);
// return: array(3) { ["id"]=> string(1) "1" ["k"]=> string(2) "k1" ["v"]=> string(2) "v1" }
$result = $result->getAttributes();
// return: NULL
$result = $model->findByPk(-1);
var_dump($result);
}
public static function testFindByAttributes(){
$model = self::model();
// return db object
$result = $model->findByAttributes(array('id'=>1),'k=:k',array(':k'=>'k1'));
// return: array(3) { ["id"]=> string(1) "1" ["k"]=> string(2) "k1" ["v"]=> string(2) "v1" }
$result = $result->getAttributes();
// return: NULL
$result = $model->findByAttributes(array('id'=>-1));
}
public static function testWithFindAll(){
$model = self::model();
// return db object
$result = $model->with('key')->findAll();
foreach($result as $row){
// return: array(3) { ["id"]=> string(1) "1" ["k"]=> string(2) "k1" ["v"]=> string(2) "v1" }
$v = $row->getAttributes();
// return null
$testmodel2row = $row->key->getAttributes();
}
}
}
?>
2.2 Test2Mode.php
<?php
/*
CREATE TABLE IF NOT EXISTS `pkrss_test2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` varchar(255) NOT NULL,
`v2` varchar(255) DEFAULT NULL,
`pid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`)
) AUTO_INCREMENT=3 ;
INSERT INTO `pkrss_test2` (`id`, `k`, `v2`, `pid`) VALUES
(1, 'k1', 'k1', 1),
(2, 'k2', 'v2', 2);
ALTER TABLE `pkrss_test2`
ADD CONSTRAINT `pkrss_test2_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `pkrss_test` (`id`);
*/
class Test2Model extends CActiveRecord {
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{test2}}';
}
public $k='';
public $v2='';
public $pid=0;
public function primaryKey()
{
return 'id';
}
public function relations()
{
return array(
'key'=>array(self::BELONGS_TO, 'TestModel', 'pid'),
);
}
public static function testWithFindAll(){
$model = self::model();
// return db obj
$result = $model->with('key')->findAll();
foreach($result as $row){
// return: array(4) { ["id"]=> string(1) "1" ["pid"]=> string(1) "1" ["k"]=> string(2) "k1" ["v2"]=> string(2) "k1" }
$v = $row->getAttributes();
// return array(3) { ["id"]=> string(1) "1" ["k"]=> string(2) "k1" ["v"]=> string(2) "v1" }
$testmodelrow = $row->key->getAttributes();
}
}
}
?>
No comments:
Post a Comment