Created
December 2, 2012 23:02
-
-
Save thoaionline/4191490 to your computer and use it in GitHub Desktop.
cannot access ServiceManager
This file contains 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 Snipplr\Model; | |
use ArrayObject; | |
use Zend\ServiceManager\ServiceManager; | |
use Zend\ServiceManager\ServiceManagerAwareInterface; | |
abstract class BaseEntity extends ArrayObject implements ServiceManagerAwareInterface{ | |
protected $_name="object"; | |
protected $serviceManager; | |
public function setServiceManager(ServiceManager $serviceManager) | |
{ | |
$this->serviceManager = $serviceManager; | |
} | |
public function getServiceManager(){ | |
return $this->serviceManager; | |
} | |
public function __get($field){ | |
if ($field=='id'){ | |
$field_name="{$this->_name}_id"; | |
return $this->$field_name; | |
}else{ | |
return $this[$field]; | |
} | |
} | |
} | |
?> |
This file contains 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
[03-Dec-2012 09:56:36] PHP Fatal error: Call to a member function get() on a non-object in /Volumes/DATA/Sites/snipplr3_studio/snipplr3/module/Snipplr/src/Snipplr/Model/Snippet.php on line 12 |
This file contains 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 Snipplr; | |
use Snipplr\Model\SnippetTable; | |
use Snipplr\Model\Snippet; | |
use Snipplr\Model\Source; | |
use Zend\ServiceManager\ServiceManager; | |
use Zend\Db\TableGateway\TableGateway; | |
use Zend\Db\ResultSet\ResultSet; | |
class Module { | |
public function getAutoLoaderConfig() { | |
return array( | |
'Zend\Loader\ClassMapAutoLoader' => array( | |
__DIR__ . '/autoload_classmap.php'), | |
'Zend\Loader\StandardAutoLoader' => array( | |
'namespaces' => array( | |
__NAMESPACE__ => __DIR__ . '/src/' | |
. __NAMESPACE__,),)); | |
} | |
public function getConfig() { | |
return include __DIR__ . '/config/module.config.php'; | |
} | |
public static function getTableGateway($sm, $tableName, $prototype) { | |
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); | |
$resultSetPrototype = new ResultSet(); | |
$resultSetPrototype->setArrayObjectPrototype($prototype); | |
return new TableGateway($tableName, $dbAdapter, null, | |
$resultSetPrototype); | |
} | |
public function getServiceConfig() { | |
return array( | |
'factories' => array( | |
'SnippetTableGateway' => function ($sm) { | |
return Module::getTableGateway($sm, 'snippets', | |
new Snippet()); | |
}, | |
'SourceTableGateway'=> function($sm){ | |
return Module::getTableGateway($sm,'sources', | |
new Source()); | |
}, | |
'Snipplr\Model\SnippetTable' => function ($sm) { | |
$tableGateway = $sm->get('SnippetTableGateway'); | |
$table = new SnippetTable($tableGateway); | |
return $table; | |
}, | |
'Snipplr\Model\SourceTable'=>function($sm){ | |
$tableGateway= $sm->get('SourceTableGateway'); | |
$table=new SourceTable($tableGateway); | |
return $table; | |
} | |
) | |
); | |
} | |
} | |
?> |
This file contains 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 Snipplr\Model; | |
class Snippet extends BaseEntity { | |
protected $_name="snippet"; | |
private $_code; | |
public function __get($field){ | |
if ($field=="code"){ | |
if (!isset($this->_code)){ | |
//TODO: Retrieve this from the DB | |
$sm=$this->getServiceManager(); | |
$source_table=$sm->get('Snipplr\Model\SourceTable'); | |
$source=$source_table->fetchByKey('snippet_id',$this->id); | |
$this->_code=$source->code; | |
} | |
return $this->_code; | |
}else{ | |
return parent::__get($field); | |
} | |
} | |
public function __set($field,$value){ | |
if ($field=="code"){ | |
//TODO: Update the db | |
$this->_code=$code; | |
}else{ | |
parent::__set($field,$value); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment