When i am doing pagination using zend:
I am getting this error:
Notice: Undefined variable: db in /var/www/ZendApp/application/controllers/ContsizesController.php on line 17
Fatal error: Call to a member function fetchAll() on a non-object in /var/www/ZendApp/application/controllers/ContsizesController.php on line 17
//Controller
$sql = 'SELECT * FROM contsize ';
$result = $db->fetchAll($sql);
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
The above mentioned code i am using in controller.
Please help me to correct it.Advance Thanks
I hope this can help you.
class PathToClass_ClassName extends Zend_Db_Table_Abstract
{
/*$_name is zend global for selecting db table*/
protected $_name = 'contsize';
public function paginationZend()
/*$this->select() by default selectingall records from database*/
$SELECT = $this->SELECT();
$result = $this->fetchAll($SELECT);
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
}
Than you can call your function where you want.
Related
My code:
public function insertMember($member)
{
$maxOrderNumber = $this->select()
->from($this, array(new Zend_Db_Expr('max(order_number)')));
$id = $this->insert($member, $maxOrderNumber);
return $id;
}
I want to insert member to last position in members table and order_number entity. Tried with $maxOrderNumber but i keep getting 0 value in database.
Im using MySql. Also i have user_id with (AI) Autoincrement so i'm forced to function this way.
public function insertMember($member)
{
$maxOrderNumber = $this->select()
->from($this, array(new Zend_Db_Expr('max(order_number)')));
$stmt = $maxOrderNumber ->query();
$result = $stmt->fetchAll();
$id = $this->insert($member, $result ['order_number']);
return $id;
}
soemthing like that...
I have the following query in php with (pdo_pgsql) adapter:
$dsn = "pgsql:host=$host;port=5432;dbname=$db;user=$username;password=$password";
$conn = new PDO($dsn);
$query = 'SET search_path TO xyz_de;';
$result = $conn->query($query);
$query = 'Select * FROM "Orders" WHERE vendor_id=189 ';
$statement = $conn->query($query);
while($row = $statement->fetch(PDO::FETCH_ASSOC)) {
var_dump($row); // --> working perfectly fine, I see results here
}
I want to reproduce it using doctrine so I'm doing the following:
// given: 1) $this->em = EntityManager
// 2) the conenction is established successfull (I debugged it and sure about it)
$rsm = new ResultSetMapping();
$query = $this->em->createNativeQuery("SET search_path TO xyz_de ", $rsm);
$query->execute();
$query = $this->em->createNativeQuery('SELECT * FROM "Orders" WHERE vendor_id=?', $rsm);
$query->setParameter(1, 189);
$orders = $query->getArrayResult();
var_dump($orders); // Gives empty array (no errors)
as per my comment in the last line, the problem is that I get no results and no errors when using doctrine for this
Many things:
You can use addScalarResult
A simple example:
$rsm->addScalarResult('my_real_column', 'my_alias_column');
$query = $this->em->createNativeQuery('SELECT my_real_column FROM "Orders"');
var_dump($query->getScalarResult());
But I think, the best you can do is to use the queryBuilder.
I am using Zend and Doctrine to login using a table containing also a foreign ID to another table. I need to get this ID in order to use it in a Doctrine query (through the controller) to the database like this:
$q = Doctrine_Query::create()
->from('Lost_Model_Item i')
->where('i.StatID = ?', 'I need the ID here')
$result = $q->fetchArray();
I have tried to get it like this:
Zend_Auth::getInstance()->getIdentity()->ID
But it seems to not work. I am new to Zend and a bit lost here. Could you please help?
As I ma working with doctrine I have created an adapter as follow:
public function authenticate()
{
$q = Doctrine_Query::create()
->from('Lost_Model_Station u')
->where('u.username = ? AND u.password = MD5(?)',
array($this->username, $this->password)
);
$result = $q->fetchArray();
if (count($result) == 1) {
$this->_resultArray = $result[0];
return new Zend_Auth_Result(
Zend_Auth_Result::SUCCESS, $this->username, array());
} else {
return new Zend_Auth_Result(
Zend_Auth_Result::FAILURE, null,
array('Authentication unsuccessful')
);
}
}
How about using getIdentity to get the username and then use a sql join on Lost_Model_Item and Lost_Model_Station
$q = Doctrine_Query::create()
->from('Lost_Model_Item i')
->leftJoin('i.Lost_Model_Station u')
->where('u.username = ?', Zend_Auth::getInstance()->getIdentity())
$result = $q->fetchArray();
This assumes that there is a doctrine "hasOne" relation defined for Lost_Model_Station (in the base class):
public function setUp()
{
parent::setUp();
$this->hasOne('Lost_Model_Item', array(
'local' => 'StatID',
'foreign' => 'whatever_id_StatID_relates_to'));
}
Zend_Auth::getInstance()->getIdentity()->ID
well you got what you store in Identity . You need to post the code where you ask user to login . Because there you are not storing the ID inside the session storage which Zend_Auth uses . Basically do this there
$row = $this->_adapter->getResultRowObject();
Zend_Auth::getInstance()->getStorage()->write($row);
where $this->_adapter is
$this->_adapter = new Zend_Auth_Adapter_DbTable();
$this->_adapter->setTableName('users')
->setIdentityColumn('email')
->setCredentialColumn('password');
im trying to use the doctrine pager but its errored:
Fatal error: Class 'Doctrine_Pager' not found
code:
$page = 1;
$results_per_page = 10;
$pager = new Doctrine_Pager(
$query,
$page,
$results_per_page
);
$results = $pager->execute(array(), Doctrine::HYDRATE_ARRAY);
$num_results = $results->getNumResults();
i usually call doctrine EM like this:
$this->_doctrine = Zend_Registry::get('doctrineEm');
$query = $this->_doctrine->createQueryBuilder()
how would i load this doctrine pager?
Its my understanding that the pager from 1.2 has been removed from doctrine2.
However, you can add "doctrine extensions" to the ORM which will give you that functionality.
https://github.com/beberlei/DoctrineExtensions
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
This is the code I'm using to pass the database handle to my class:
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($db->connect_error)
die('MySQL connection error (' . $db->connect_errno . ') ' . $db->connect_error);
$linkgen = new Linkgen($db);
echo $linkgen->getLinksLeft();
And this is the code in the Linkgen class:
class Linkgen {
private $db; // our database handle
private $linksLeft;
function __contruct($db)
{
$this->db = $db;
}
function getLinksleft()
{
$ip = $_SERVER['REMOTE_ADDR'];
$q = $this->db->prepare("SELECT links_left FROM `limits` WHERE ip=?");
$q->bind_param("s", $ip);
$q->execute();
$q->bind_result($this->linksLeft);
$q->fetch();
printf("%i links left", $this->linksLeft);
$q->close();
}
}
For some reason I get the non-object error when I call the getLinksLeft method. I can't figure this out because from what I can see I'm referencing the database handle correctly.
Any help is appreciated, thanks.
Why not extend your object to become apart of mysqli?
class Linkgen extends mysqli
{
private $_linksLeft;
function __construct()
{
parent::__construct(DB_HOST, DB_USER, DB_PASS, DB_NAME);
}
function getLinksleft()
{
$ip = $_SERVER['REMOTE_ADDR'];
$q = $this->prepare("SELECT links_left FROM `limits` WHERE ip=?");
$q->bind_param("s", $ip);
$q->execute();
$q->bind_result($this->_linksLeft);
$q->fetch();
printf("%i links left", $this->_linksLeft);
$q->close();
}
//...
}
//Usage:
$Linkgen = new Linkgen();
echo $Linkgen->getLinksLeft();
And if you still want your error fixed then you have miss spelled __construct ;)
it is because your $db in the Linkgen class is null or unset. You need to check it before you invoke getLinksleft().