1

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?

1 Answer 1

1

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;         
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot , and how to show result? maybe , I must create new Views, yes?
Yes to display results you need to create views.
I would do this return $this->fetchAll($this->select()->where("something = ?, $something));. This will return Zend_Db_Table_Rowset containing Zend_Db_Table_Row which can be extended (for example to create user class so quest returns user objects)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.