Title says it.
It says that i dont have a columnd 'user_id' but i have. It works with 'id'.
This is the problem function :
public function getUploadsByUserId($userId)
{
$userId = (int) $userId;
$rowset = $this->tableGateway->select(
array('user_id' => $userId));
return $rowset;
}
This is the model:
namespace Users\Model;
use Zend\View\Model\ConsoleModel;
class Upload
{
public $id;
public $filename;
public $label;
public $user_id;
public function getArrayCopy()
{
return get_object_vars($this);
}
function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->filename = (isset($data['filename'])) ? $data['filename'] : null;
$this->label = (isset($data['label'])) ? $data['label'] : null;
$this->user_id = (isset($data['user_id'])) ? $data['user_id'] : null;
}
}
Where can be the problem here, its clearly that i have such column.
The problem was that i was not configured the tablegateway correctly. It was 'user' instead of 'uploads' .
'UploadTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Upload());
return new TableGateway('uploads', $dbAdapter, null,
$resultSetPrototype);
},
Related
Is it possible to nest aggregate hydrators? If i have the following classes:
class Appointment{
public date;
public startTime;
public endTime;
public User; //* #var User */
}
class User{
public Location; //* #var Location*/
}
...being populated with the following AggregateHydrator (created from a factory):
class AppointmentModelHydratorFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator) {
$serviceManager = $serviceLocator->getServiceLocator();
$arrayHydrator = new ArraySerializable();
$arrayHydrator->addStrategy('date', new DateTimeStrategy())
->addStrategy('endTime', new TimeStrategy())
->addStrategy('startTime', new TimeStrategy());
$aggregateHydrator = new AggregateHydrator();
$aggregateHydrator->add($arrayHydrator);
$aggregateHydrator->add($serviceLocator->get('Hydrator\User'));
return $aggregateHydrator;
}
}
With the UserHydratorFactory looking like:
class UserHydratorFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator) {
$sm = $serviceLocator->getServiceLocator();
$userHydrator = new UserHydrator($sm->get('User\Mapper'));
$aggregateHydrator = new AggregateHydrator();
$aggregateHydrator->add($userHydrator );
$aggregateHydrator->add($sm->get('HydratorManager')->get('Hydrator\User\Location'));
return $aggregateHydrator;
}
}
This is throwing an expection as the model is being returned as null, but if i comment out adding the Location hydrator to the User hydrator, it works fine (albeit without location data loaded). So i was wondering if aggregate hydrators are able to be nested?
It is not built-in, but doable.
namespace Hydrator;
use Zend\Stdlib\Hydrator\HydratorInterface;
class NestedHydrator implements HydratorInterface
{
protected $inner_hydrator;
private $empty;
public function __construct ($inner_hydrator, $empty)
{
$this->inner_hydrator = $inner_hydrator;
$this->empty = $empty;
}
public function extract ($object)
{
return [
$this->getPath() => $this->inner_hydrator->extract ($object->{$this->getPath()})
];
}
public function hydrate (array $data, $object)
{
$object->{$this->getPath()} = $this->inner_hydrator->hydrate ($data [$this->getPath()], $this->empty);
return $object;
}
protected function getPath ()
{
return get_class ($this->empty);
}
}
And then:
$u = new User();
$u->Location = "4 Clinton Rd.";
$a = new Appointment();
$a->date = "yesterday";
$a->startTime = "7:00";
$a->endTime = "8:00";
$a->User = $u;
$h = new AggregateHydrator();
$h->add (new ObjectProperty());
$nested = new \Hydrator\NestedHydrator(new ObjectProperty(), new User());
$h->add ($nested);
$data = $h->extract ($a);
$b = $h->hydrate ($data, new Appointment());
$this->assertEquals ($a, $b);
I have a project with structure: https://docs.google.com/file/d/0B6hBvUW6YcomNHZJYUxvbTgxNkE
This is my AbstractTable.php:
namespace AdminManage\Model;
use Zend\Db\Adapter\Adapter;
use Zend\ServiceManager\ServiceLocatorInterface;
class AbstractTable{
protected $tablegateway;
protected $table;
protected $serviceLocator;
protected $adapter;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
$this->table = $this->tableGateway->getTable();
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator){
$this->serviceLocator = $serviceLocator;
return $this;
}
public function getServiceLocator(){
return $this->serviceLocator;
}
public function setDbAdapter(Adapter $adapter){
$this->adapter = $adapter;
return $this;
}
public function getDbAdapter(){
return $this->adapter;
}
public function fetchAll(){
return $this->tablegateway->select();
}
public function getTable(){
if(!$this->table)
$this->table = $this->tableGateway->getTable();
return $this->table;
}
}
This is my CategoryTable.php:
namespace Category\Model;
use Zend\Db\Sql\Sql;
use AdminManage\Model\AbstractTable;
class CategoryTable extends AbstractTable {
public function getListCategory() {
$adapter = $this->getDbAdapter ();
$sql = new Sql ( $adapter );
$select = $sql->select ( array (
"c" => "category"
) )->join ( array (
"cd" => "category_description"
), "c.category_id = cd.category_id" );
echo $sql->prepareStatementForSqlObject ( $select )->getSql ();
}
}
When I call getListCategory() in the controller, has an error:
Fatal error: Class 'AdminManage\Model\AbstractTable' not found in D:\xampp\htdocs\dokusyu\htdocs\admin\dokusyu_be\module\Course\src\Category\Model\CategoryTable.php on line 5.
How can I fix this error? Thank you!
ZF2 refer all files from root folder of your host, try creating a subdomain and and copy the files and access the pages
I've got a fatal error when I intent insert a row in DB. I don't understand what's happening, I readed some blogs but there is not a solution, my code is the same like an example publicated by Evan in his blog.
My model class
class CommentTable
{
protected $_commentTableGateway;
protected $_hydratator;
protected $_resultSet;
public function __construct($adapter)
{
$this->_hydratator = new \Zend\Stdlib\Hydrator\ClassMethods;
$rowObjectPrototype = new Comment();
$this->_resultSet = new \Zend\Db\ResultSet\HydratingResultSet($this->_hydratator, $rowObjectPrototype);
$this->_commentTableGateway = new TableGateway('comments', $adapter, null, $this->_resultSet );
}
public function fetchAll()
{
return $this->_commentTableGateway->select();
}
public function saveComment(Comment $comment)
{
$id = (int)$comment->getId();
if ($id == 0) {
$this->_commentTableGateway->insert($this->_hydratator->extract($comment));//this fails
} else {
if ($this->getComment($id)) {
$this->_commentTableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('El comentario que queire editar no exite');
}
}
}
public function getComment($id)
{
$id = (int) $id;
$rowset = $this->_commentTableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
}
</code>
<code>
In module class:
//a factory in service manager
'Comment\Model\CommentTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new CommentTable($dbAdapter);
return $table;
},
</code>
<code>
My controller:
public function getCommentTable()
{
if (!$this->_commentTable) {
$sm = $this->getServiceLocator();
$this->_commentTable = $sm->get('Comment\Model\CommentTable');
}
return $this->_commentTable;
}
</code>
And I get this error:
Catchable fatal error: Object of class stdClass could not be converted to string in D:\xampp\htdocs\haystack\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php on line 258
I know the type of the error('stdClass could not be converted to string'), but I don't understand what's happening...
Any help is appreciated
Kind regards.
Hopefully this will help you out. Here is my TableGateway
class Domains extends AbstractTableGateway
{
public function __construct($adapter)
{
$this->table = 'domains';
$this->adapter = $adapter;
$this->initialize();
}
}
And here is how I insert data:
$this->getTableDomains()->insert(array(
'companyid' => $params['companyid'],
'domain_id' => $result->id,
'name' => $name,
'type' => strtoupper($params['type']),
'content' => strtolower($params['content']),
'ttl' => $ttl,
'prio' => $prio
));
I got a ZF2 project with 2 Models PosTable/TopTable which extend AbstractTableGateway.
I want to Paginate results from those Tables so i have a Pagination function in both of them.
this is what the PosTable Model looks like:
...
class PosTable extends AbstractTableGateway {
public function __construct($adapter) {
$this->table = 'pos';
$this->adapter = $adapter;
}
...
public function getPosPaginator($tid) {
$sql = $this->getSql();
$select = $sql->select();
$select->where('tid = '.$tid)->where('deleted = 0')->order('crdate ASC');
$adapter = new \Zend\Paginator\Adapter\DbSelect($select, $sql);
$paginator = new \Zend\Paginator\Paginator($adapter);
return $paginator;
}
...
which works perfectly.
but in my TopTable it looks the same like this:
...
class TopTable extends AbstractTableGateway {
public function __construct($adapter) {
$this->table = 'top';
$this->adapter = $adapter;
}
public function getTopPaginator($fid) {
$sql = $this->getSql();
$select = $sql->select();
$select->where('fid = '.$fid)->where('deleted = 0');
$adapter = new \Zend\Paginator\Adapter\DbSelect($select, $sql);
$paginator = new \Zend\Paginator\Paginator($adapter);
return $paginator;
}
...
my controller looks like this for PosTable:
...
public function posAction(){
...
$pos = $this->getPosTable()->getPosPaginator($tid);
$pos->setCurrentPageNumber($pageid)->setItemCountPerPage(19);
... return $pos etc...
same controller topAction:
...
public function topAction(){
...
$top = $this->getTopTable()->getTopPaginator($fid);
$top->setCurrentPageNumber($pageid)->setItemCountPerPage(20);
...return $top etc..
in that controller i got also these functions:
public function getTopTable(){
return $this->getServiceLocator()->get('Application\Model\TopTable');
}
public function getPosTable(){
return $this->getServiceLocator()->get('Application\Model\PosTable');
}
PosTable Pagination works perfectly, but the TopTable Pagination doesnt work.
i get this error:
Fatal error: Call to a member function select() on a non-object in ....
seems like
$sql = $this->getSql();
doesnt return the object.
how can i solve this problem?
one works one doesnt for no obvious reason.
my module.php looks like this:
namespace Application;
class Module
{
public function getAutoloaderConfig()
{
return array('Zend\Loader\StandardAutoloader' =>
array('namespaces' =>
array(__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Application\Model\TopTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new \Application\Model\TopTable($dbAdapter);
return $table;
},
'Application\Model\ForTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new \Application\Model\ForTable($dbAdapter);
return $table;
},
'Application\Model\PosTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new \Application\Model\PosTable($dbAdapter);
return $table;
},
),
);
}
}
Ok, after going through TableGateway Code and your extended class, I found the your implementation is not calling initialize which setup the sql object try to call the parent table gateway, below is the modified constructor for your PosTable and TopTable
/* for PosTable */
class PosTable extends TableGateway {
public function __construct($adapter) {
parent::__construct('pos', $adapter);
}
...
/* for TopTable */
class TopTable extends TableGateway {
public function __construct($adapter) {
parent::__construct('top', $adapter);
}
...
I use symfony 1.4.11 .I have next component class:
class companiesComponents extends sfComponents {
public function executeCompanylist(sfWebRequest $request) {
// And the URL
if (!isset($this->url)) {
throw new Exception('Please specify the URL');
}
// Save the page
if ($request->getParameter('page')) {
$this->setPage($request->getParameter('page'));
}
// Create pager
$this->pager = new sfDoctrinePager('Companies', sfConfig::get('app_ads_per_page', 5));
$this->pager->setQuery($this->query);
$this->pager->setPage($this->getPage());
$this->pager->init();
}
protected function getPager($query) {
$pager = new Doctrine_Pager($query, $this->getPage(), 3);
return $pager;
}
protected function setPage($page) {
$this->getUser()->setAttribute('users.page', $page, 'admin_module');
}
protected function getPage() {
return $this->getUser()->getAttribute('users.page', 1, 'admin_module');
}
I have action:
public function executeAll(sfWebRequest $request)
{
$this->query = Doctrine_Core::getTable('Companies')->getAllCompany();
$this->url = '#companylist';
}
And I have allSucess.php
<?php include_component('companies', 'companylist', array(
'query' => $query,
'url' => $url,
'noneFound' => __('You haven\'t created any ads yet.')
)) ?>
In my Companies Table class
public function getAllCompany()
{
$q = $this->createQuery('a')
->andWhere('a.active = ?',1)
->leftJoin('a.Owner o')
->leftJoin('o.Profile p')
->andWhere('p.payed_until > NOW()')
->addORDERBY ('created_at DESC');
}
And it is do not work. I get all my record "companies" from database,but they are not selected according to the my query...
When I make
public function getAllCompany()
{
}
or when I comment
// $this->pager->setQuery($this->query);
I still get all my records :(
In logs I see :
Template: companies … allSuccess.php
Parameters:
$query (NULL)
$url (string)
When I make
public function getAllCompany()
{
$q = $this->createQuery('a')
->andWhere('a.active = ?',1)
->leftJoin('a.Owner o')
->leftJoin('o.Profile p')
->andWhere('p.payed_until > NOW()')
->addORDERBY ('created_at DESC');
return $q->execute();
}
I have error:
Fatal error: Call to undefined method Doctrine_Collection::offset()
I do not understand how it get all records, and where I made mistake :(
Thank you!
remove the ->execute(); text from the return statement in the getAllCompany() function ... the DoctrinePager executes the statement - you don't need to ...
public function getAllCompany()
{
$q = $this->createQuery('a')
->andWhere('a.active = ?',1)
->leftJoin('a.Owner o')
->leftJoin('o.Profile p')
->andWhere('p.payed_until > NOW()')
->addOrderBy('created_at DESC');
return $q;
}