Created
March 9, 2011 16:45
-
-
Save harikt/862514 to your computer and use it in GitHub Desktop.
Table Structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use lithium\data\Connections; | |
Connections::get("default")->applyFilter("_execute", function($self, $params, $chain) { | |
var_dump($params['sql']); | |
return $chain->next($self, $params, $chain); } | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
class Comments extends \lithium\data\Model { | |
public $validates = array(); | |
public $belongsTo = array('Posts','Users'); | |
protected $_schema = array( | |
'id' => array('type' => 'integer', 'key' => 'primary'), | |
'user_id' => array('type'=>'integer'), | |
'post_id' => array('type'=>'integer'), | |
'comment' => array('type' => 'text'), | |
'updated' => array('type' => 'datetime'), | |
'status' => array('type' => 'integer') | |
); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Table structure for table `comments` | |
-- | |
DROP TABLE IF EXISTS `comments`; | |
/*!40101 SET @saved_cs_client = @@character_set_client */; | |
/*!40101 SET character_set_client = utf8 */; | |
CREATE TABLE `comments` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`user_id` int(11) NOT NULL, | |
`post_id` int(11) NOT NULL, | |
`comment` text NOT NULL, | |
`updated` datetime NOT NULL, | |
`status` int(11) NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; | |
/*!40101 SET character_set_client = @saved_cs_client */; | |
-- | |
-- Table structure for table `users` | |
-- | |
DROP TABLE IF EXISTS `posts`; | |
/*!40101 SET @saved_cs_client = @@character_set_client */; | |
/*!40101 SET character_set_client = utf8 */; | |
CREATE TABLE `posts` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`title` varchar(100) NOT NULL, | |
`body` text NOT NULL, | |
`url` varchar(100) NOT NULL, | |
`user_id` int(11) NOT NULL DEFAULT '0', | |
`created` datetime NOT NULL, | |
`updated` datetime NOT NULL, | |
`status` int(11) NOT NULL DEFAULT '0', | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 COMMENT='Posts table'; | |
/*!40101 SET character_set_client = @saved_cs_client */; | |
-- | |
-- Table structure for table `users` | |
-- | |
DROP TABLE IF EXISTS `users`; | |
/*!40101 SET @saved_cs_client = @@character_set_client */; | |
/*!40101 SET character_set_client = utf8 */; | |
CREATE TABLE `users` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`username` varchar(50) NOT NULL, | |
`password` varchar(50) NOT NULL, | |
`fullname` varchar(100) NOT NULL, | |
`url` varchar(50) DEFAULT NULL, | |
`address_id` int(11) DEFAULT NULL, | |
`email` varchar(255) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 COMMENT='List of users of website'; | |
/*!40101 SET character_set_client = @saved_cs_client */; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
class Posts extends \lithium\data\Model { | |
public $validates = array( | |
'title' => array('notEmpty', 'message' => 'You need a title'), | |
'body' => array('notEmpty', 'message' => 'You need a body') | |
); | |
public $hasMany = array( | |
'Comments'=>array( | |
'key' => array('id'=>'post_id'), | |
'conditions' => array('Comments.status' => '1'), | |
'order' => 'Comments.created DESC', | |
) | |
//'Tagging'=>array('keys' => array('id'=>'post_id')) | |
); | |
public $belongsTo = array('Users'); | |
/* | |
public $belongsTo = array( | |
'Users' => array( | |
'key' => array('user_id'=>'id') | |
) | |
); | |
*/ | |
protected $_schema = array( | |
'id' => array( 'type'=>'integer', 'key'=> 'primary'), | |
'title' => array('type'=>'string'), | |
'body' => array('type'=>'text'), | |
'url' => array('type'=>'string'), | |
'user_id' => array('type'=>'integer'), | |
'created' => array('type'=>'datetime'), | |
'updated' => array('type'=>'datetime'), | |
'status' => array('type'=>'integer') | |
); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
class Users extends \lithium\data\Model { | |
public $validates = array( | |
'fullname' => 'Please enter a full name', | |
'username' => 'Please enter a user name', | |
'email' => array( | |
array('notEmpty', 'message' => 'email is empty'), | |
array('email', 'message' => 'email is not valid'), | |
) | |
); | |
public $hasMany = array( | |
'Posts' => array('key'=>array('id'=>'post_id')), | |
'Comments' => array('key'=>array('id'=>'comment_id')) | |
); | |
protected $_schema = array( | |
'id' => array('type'=>'integer', 'key'=>'primary'), | |
'username' => array('type'=>'string'), | |
'password' => array('type'=>'string'), | |
'fullname' => array('type'=>'string'), | |
'url' => array('type'=>'string'), | |
'address_id' => array('type'=>'integer'), | |
'email'=>array('type'=>'integer') | |
); | |
} | |
Users::applyFilter('save', function($self, $params, $chain){ | |
$record = $params['entity']; | |
if (!$record->id) { | |
$record->password = \lithium\util\String::hash($record->password); | |
} | |
if (!empty($params['data'])) { | |
$params['data']['password'] = \lithium\util\String::hash($params['data']['password']); | |
$record->set($params['data']); | |
} | |
$params['entity'] = $record; | |
return $chain->next($self, $params, $chain); | |
}); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function view() { | |
// Select all fields from the both table | |
$post = Posts::find('first', array( | |
'conditions'=> array('Posts.id'=>$this->request->id), | |
'with'=>array('Users'))); | |
//Don't want to fetch certain datas like password, credit card like stuffs ?, use fields :) | |
/* | |
$post = Posts::find('first', array( | |
'conditions'=> array('Posts.id'=>$this->request->id), | |
'fields'=>array( | |
'Posts'=>array('id','title','body','created'), | |
'Users'=>array('id','fullname'),), | |
'with'=>array('Users'))); | |
*/ | |
if (!$post) { | |
return $this->redirect('Posts::index'); | |
} | |
$comments = Comments::find('all',array( | |
'conditions'=>array('post_id'=>$this->request->id), | |
'with'=>array('Users') | |
)); | |
/* | |
//Helpful when you want to dump the data ->data(); | |
echo '<pre>'; | |
print_r( $post->data() ); | |
print_r( $comments->data() ); | |
echo '</pre>'; | |
*/ | |
return compact('post', 'comments'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Little proposal:
For portability iwould'nt it be nicer if we use $model->exists() instead of checking against the id? (mongo: _id ...)