Farzan Najipour
Farzan Najipour

Reputation: 2503

Zend Framework: Select query

I'm newbie on Zend Framework, I created DbTable , my primary key is id , and my table name is user:

<?php

class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{

    protected $_name = 'user';
    protected $_primary = 'id';

}

after that ,I reviewed Abstract.php(Db/Table/Abstract.php) and I found out that I had to use insert(array $data), so I created a model: (Register.php)

<?php

class Application_Model_Register
{
    public function CreateUser($array)
    {
        $dbTableUser = new Application_Model_DbTable_User();
        $dbTableUser -> insert($array);
    }

}

and finally , in my Controllers , I created IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $register = new Application_Model_Register();
        $register-> CreateUser(array(
               'username'=>'test'));
    }
}

It works correctly , but I have no idea about Select, How I select query from user table?

Upvotes: 1

Views: 4656

Answers (1)

Nikul
Nikul

Reputation: 1025

our controller should have to be like below

<?php
class IndexController extends Zend_Controller_Action
{
    public function getdataAction()
    {
         $register = new Application_Model_Dbtable_Register();
         $register->getListOfUser(); 
    }
}

Now your model should have to be like this,

<?php

class Application_Model_DbTable_Register extends Zend_Db_Table_Abstract
{

    protected $_name = 'user';
    protected $_primary = 'id';


    public function getListOfUser()
    {
         $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $select  = $db->select()
                      ->from($_name,array('*'))
                      ->where(1);
        $data = $db->query($select)->fetchAll();
        return $data;         
    }
}

Upvotes: 1

Related Questions