Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure if it's the best practice, but, this is my abstract mapper:</p> <pre><code>&lt;?php abstract class Zf_Model_DbTable_Mapper { protected $_db; protected $_dbTable = null; protected $_systemLogger = null; protected $_userLogger = null; public function __construct() { $this-&gt;_systemLogger = Zend_Registry::get('systemLogger'); $this-&gt;_userLogger = Zend_Registry::get('userLogger'); // Set the adapter if(null !== $this-&gt;_dbTable) { $tableName = $this-&gt;_dbTable; $this-&gt;_db = $this-&gt;$tableName-&gt;getAdapter(); } } public function __get($value) { if(isset($this-&gt;$value)) { return $this-&gt;$value; } $dbTable = 'Model_DbTable_' . $value; $mapper = 'Model_' . $value; if(class_exists($dbTable)) { return new $dbTable; } elseif(class_exists($mapper)) { return new $mapper; } else { throw new Exception("The property, DbTable or Mapper \"$value\" doesn't exists"); } } public function __set($key,$value) { $this-&gt;$key = $value; } public function getById($id) { $resource = $this-&gt;getDefaultResource(); $id = (int)$id; $row = $resource-&gt;fetchRow('id =' . $id); if (!$row) { throw new Exception("Count not find row $id"); } return $row; } public function getAll() { $resource = $this-&gt;getDefaultResource(); return $resource-&gt;fetchAll()-&gt;toArray(); } public function save(Zf_Model $Model) { $dbTable = $this-&gt;getDefaultResource(); $data = $Model-&gt;toArray(); if(false === $data) return false; if(false === $Model-&gt;isNew()) { if(1 == $dbTable-&gt;update($data, 'id =' . (int)$Model-&gt;getId())) { return $Model; } } else { $id = $dbTable-&gt;insert($data); if($id) { $Model-&gt;setId($id); return $Model; } } return false; } public function remove($id) { return $this-&gt;getDefaultResource()-&gt;delete('id =' . (int) $id); } protected function getDefaultResource() { if(empty($this-&gt;_dbTable)) { throw new Exception('The $_dbTable property was not set.'); } $classname = 'Model_DbTable_' . $this-&gt;_dbTable; if(!class_exists($classname)) { throw new Exception("The Model_DbTable_\"$classname\" class was not found."); } return new $classname; } protected function getDefaultModel() { return current($this-&gt;_models); } protected function getResources() { return $this-&gt;_resources; } } </code></pre> <p>And this is one for my implemented mappers:</p> <pre><code>&lt;?php class Model_TwitterPostsMapper extends Zf_Model_DbTable_Mapper { /* * Data Source * @var string Zend_Db_Table name */ protected $_dbTable = 'TwitterPosts'; public function recordExists($Item) { $row = $this-&gt;TwitterPosts-&gt;fetchRow($this-&gt;TwitterPosts-&gt;select()-&gt;where('status_id =?', $Item-&gt;getSource()-&gt;getStatusId())); if($row) { return $row-&gt;id; } return false; } public function getLastUpdate($options) { $select = $this-&gt;TwitterPosts-&gt;select() -&gt;setIntegrityCheck(false) -&gt;from(array('t' =&gt; 'twt_tweets'), 't.created_at') -&gt;join(array('u' =&gt; 'twt_users'), 't.user_id = u.id', '') -&gt;order('t.created_at DESC'); if($options['user_id']) { $select-&gt;where("t.user_id = ?", $options['user_id']); } if($options['terms']) { if(is_array($options['terms'])) { $condition = ''; foreach($options['terms'] as $i =&gt; $term) { $condition .= ($i &gt; 0) ? ' OR ' : ''; $condition .= $this-&gt;getAdapter()-&gt;quoteInto('content LIKE ?',"%$term%"); } if($condition) { $select-&gt;where($condition); } } } return $this-&gt;TwitterPosts-&gt;fetchRow($select)-&gt;created_at; } public function getSinceId($term = null) { $select = $this-&gt;TwitterPosts-&gt;select()-&gt;setIntegrityCheck(false) -&gt;from('twt_tweets_content', 'status_id') -&gt;where('MATCH(content) AGAINST(? IN BOOLEAN MODE)', "$term") -&gt;order('status_id ASC') -&gt;limit(1); //echo $select; exit; $tweet = $this-&gt;TwitterPosts-&gt;fetchRow($select); if(null !== $tweet) return $tweet-&gt;status_id; return 0; } public function getAllByStatusId($statuses_id) { $select = $this-&gt;TwitterPosts-&gt;select() -&gt;setIntegrityCheck(false) -&gt;from(array('t' =&gt; 'twt_tweets'), array('t.id', 't.user_id', 't.status_id','t.user_id')) -&gt;join(array('u' =&gt; 'twt_users'), 't.user_id = u.id', array('u.screen_name', 'u.profile_image')) -&gt;where('status_id IN(?)', $statuses_id); $rows = $this-&gt;TwitterPosts-&gt;fetchAll($select); $Posts = array(); foreach($rows as $row) { // Here we populate the models only with the specific method return data $data = $row-&gt;toArray(); $Post = new Model_TwitterPost($data['id']); $Post-&gt;populate($data); $User = new Model_TwitterUser($data['user_id']); $User-&gt;populate($data); $Post-&gt;setUser($User); $Posts[] = $Post; } return $Posts; } public function getAllSince($since_id) { $select = $this-&gt;TwitterPosts-&gt;select() -&gt;setIntegrityCheck(false) -&gt;from(array('t' =&gt; 'twt_tweets'), array('t.status_id','t.user_id')) -&gt;join(array('u' =&gt; 'twt_users'), 't.user_id = u.id', array('u.screen_name', 'u.profile_image')) -&gt;where('status_id &gt; ?', $since_id) -&gt;order('t.datetime DESC'); $rows = $this-&gt;TwitterPosts-&gt;fetchAll($select); $Posts = array(); foreach($rows as $row) { // Here we populate the models only with the specific method return data // TODO: This is not a truly lazy instatiation, since there's no way to get the not setted properties $data = $row-&gt;toArray(); $Post = new Model_TwitterPost($data); $User = new Model_TwitterUser($data); $Post-&gt;setUser($User); $Posts[] = $Post; } return $Posts; } public function getTotalRatedItems($options) { $options = $this-&gt;prepareOptions($options); $select = $this-&gt;TwitterPosts-&gt;select() -&gt;setIntegrityCheck(false) -&gt;from(array('t' =&gt; 'twt_tweets'), array('COUNT(DISTINCT t.id) AS total','r.rate')) -&gt;join(array('u' =&gt; 'twt_users'), 't.user_id = u.id', '') -&gt;join(array('r' =&gt; 'twt_tweets_rate'), 't.id = r.tweet_id', array('r.rate')) -&gt;group('r.rate') -&gt;order('t.datetime DESC'); $select = $this-&gt;prepareSelect($select, $options); $rates = $this-&gt;TwitterPosts-&gt;fetchAll($select)-&gt;toArray(); $itemsRated = array('Green' =&gt; 0, 'Yellow' =&gt; 0, 'Orange' =&gt; 0, 'Red' =&gt; 0, 'Gray' =&gt; 0); foreach ($rates as $rate) { $itemsRated[$rate['rate']] = $rate['total']; } return $itemsRated; } public function getUsersActivity($options) { $options = $this-&gt;prepareOptions($options); $select = $this-&gt;TwitterPosts-&gt;select() -&gt;setIntegrityCheck(false) -&gt;from(array('t' =&gt; 'twt_tweets'), array('COUNT(DISTINCT t.id) AS total','DATE(t.datetime) AS datetime')) -&gt;join(array('u' =&gt; 'twt_users'), 't.user_id = u.id', '') -&gt;joinLeft(array('r' =&gt; 'twt_tweets_rate'), 't.id = r.tweet_id', '') -&gt;group('t.user_id') -&gt;order('t.datetime DESC'); $select = $this-&gt;prepareSelect($select, $options); $activity = $this-&gt;TwitterPosts-&gt;fetchAll($select)-&gt;toArray(); return $activity; } public static function prepareOptions($options) { if(!is_array($options)) { $options = array(); } date_default_timezone_set('America/Sao_Paulo'); if(Zend_Date::isDate($options['start_date'])) { $date = new Zend_Date($options['start_date']); $date-&gt;setTime('00:00:00'); $date-&gt;setTimezone('UTC'); $options['start_date'] = $date-&gt;toString('yyyy-MM-dd HH:mm:ss'); } if(Zend_Date::isDate($options['end_date'])) { $date = new Zend_Date($options['end_date']); $date-&gt;setTime('23:59:59'); $date-&gt;setTimezone('UTC'); $options['end_date'] = $date-&gt;toString('yyyy-MM-dd HH:mm:ss'); } date_default_timezone_set('UTC'); $options['mainTerms'] = array(); if(!empty($options['terms']) &amp;&amp; !is_array($options['terms'])) { $options['mainTerms'] = explode(' ', $options['terms']); } if(!is_array($options['terms'])) { $options['terms'] = array(); } if($options['group_id'] || $options['client_id']) { $TwitterSearches = new Model_DbTable_TwitterSearches(); $options['terms'] = array_merge($TwitterSearches-&gt;getList($options),$options['terms']); if(empty($options['terms'])) { $options['terms'] = array(); } } return $options; } public static function prepareSelect($select, $options) { if($options['start_date']) { $select-&gt;where('t.datetime &gt;= ?', $options['start_date']); } if($options['end_date']) { $select-&gt;where('t.datetime &lt;= ?', $options['end_date']); } foreach($options['mainTerms'] as $mainTerm) { $select-&gt;where('t.content LIKE ?', "%$mainTerm%"); } if($options['user_id']) { $select-&gt;where("t.user_id = ?", $options['user_id']); } if($options['terms']) { $select-&gt;where('MATCH (t.content) AGASINT(?)', $options['terms']); } if($options['rate']) { if($options['rate'] == 'NotRated') { $select-&gt;where('r.rate IS NULL'); } else { $select-&gt;where('r.rate = ?', $options['rate']); } } if($options['last_update']) { $select-&gt;where('t.created_at &gt; ?', $options['last_update']); } if($options['max_datetime']) { $select-&gt;where('t.created_at &lt; ?', $options['max_datetime']); } return $select; } } </code></pre> <p><strong>The Model:</strong></p> <pre><code>&lt;?php class Model_TwitterPost extends Zf_Model { private $_name = 'twitter'; protected $_properties = array( 'id', 'status_id', 'user_id', 'content' ); protected $_User = null; public function setUser(Zf_Model $User) { $this-&gt;_User = $User; } public function getUser() { return $this-&gt;_User; } public function getPermalink() { return 'http://twitter.com/' . $this-&gt;screen_name . '/' . $this-&gt;status_id; } public function hasTerm($term) { if(preg_match("/\b$term\b/i", $this-&gt;getContent())) { return true; } return false; } public function getEntityName() { return $this-&gt;_name; } public function getUserProfileLink() { return $this-&gt;getUser()-&gt;getProfileLink() . '/status/' . $this-&gt;getStatusId(); } } </code></pre> <p><strong>Abstract model (Generic Object):</strong></p> <pre><code>&lt;?php abstract class Zf_Model { protected $_properties = array(); protected $_modified = array(); protected $_data = array(); protected $_new = true; protected $_loaded = false; public function __construct($id=false) { $id = (int)$id; if(!empty($id)) { $this-&gt;_data['id'] = (int)$id; $this-&gt;setNew(false); } } public function populate($data) { if(is_array($data) &amp;&amp; count($data)) { foreach($data as $k =&gt; $v) { if(in_array($k,$this-&gt;_properties)) { $this-&gt;_data[$k] = $v; } } } $this-&gt;setLoaded(true); } public function setNew($new=true) { $this-&gt;_new = (bool)$new; } public function isNew() { return $this-&gt;_new; } public function setLoaded($loaded = true) { $this-&gt;_loaded = (bool)$loaded; } public function isLoaded() { return $this-&gt;_loaded; } public function __call($methodName, $args) { if(method_exists($this, $methodName)) { return $this-&gt;$methodName($args); } $property = $methodName; if (preg_match('~^(set|get)(.*)$~', $methodName, $matches)) { $filter = new Zend_Filter_Word_CamelCaseToUnderscore(); $property = strtolower($filter-&gt;filter($matches[2])); if(in_array($property, $this-&gt;_properties)) { if('set' == $matches[1]) { $this-&gt;_data[$property] = $args[0]; if(true === $this-&gt;isLoaded()) { $this-&gt;_modified[$property] = true; } return $this; } elseif('get' == $matches[1]) { if(array_key_exists($property, $this-&gt;_data)) { return $this-&gt;_data[$property]; } throw new Exception("The property $property or $methodName() method was not setted for " . get_class($this)); } } } throw new Exception("The property '$property' doesn't exists."); } public function __get($key) { if(isset($this-&gt;_data[$key])) { return $this-&gt;_data[$key]; } return $this-&gt;$key; } public function __set($key,$value) { if(array_key_exists($key,$this-&gt;_properties)) { $this-&gt;_data[$key] = $value; return; } $this-&gt;$key = $value; } public function getId() { return (!$this-&gt;_data['id']) ? null : $this-&gt;_data['id']; } public function toArray() { // If it's a new object if(true === $this-&gt;isNew()) { return $this-&gt;_data; } // Else, if it's existing object $data = array(); foreach($this-&gt;_modified as $k=&gt;$v) { if($v) { $data[$k] = $this-&gt;_data[$k]; } } if(count($data)) { return $data; } return false; } public function reload() { $this-&gt;_modified = array(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload