views:

253

answers:

2

we tried to do like this,but it is showing some errors.Our table names are users and messages.

<?php

class Application_Model_childconnect1 extends Zend_Db_Table_Abstract
{
    protected $_name = 'users';

    public function loginvalidation($username,$pwd)
    {


        $row = $this->fetchRow('UserName = \'' . $username . '\'and UserPW = \''. $pwd . '\'');
        if (!$row)
        {
            $msg="invalid";
            return $msg;
        }
        else
        {
            return $row->toArray();
        }

    }

    protected $_name = 'messages';

     public function replymessage($message)
      {

        $data=array(
            'MessageText'=>$message
              );
        $this->insert($data);
      }


}
+1  A: 

Zend_Db_Table is a Table Data Gateway, which by definition is

An object that acts as a Gateway to a database table. One instance handles all the rows in the table.

which in turn means you have one class per table and cannot have multiple names per class. See the Zend Framework Reference Guide to learn how to use it properly:

Gordon
thanks,but it will be a very difficult task if we have many tables.so what is the solution for that.Please reply.
karthik
@karthik Well, who said programming was easy? I suggest to learn about the four common Data Source Architectural Patterns: Table Data Gateway, Row Data Gateway, Active Record and Data Mapper. See http://martinfowler.com/eaaCatalog/index.html - once you understood them, go with one of them and/or consider using an ORM like Doctrine or Propel. Or use Zend_Db and handcraft all queries.
Gordon
A: 

While you are welcome to setup the application as you have, you may want to look into using Zend Auth to handle authentication.

That aside if you are going to be making use of ZF in general then taking advantage of the Zend_Db_Select class can be helpful.

<?php
public function loginvalidation($username,$pwd)
{
  $s = $this->select();
  $s->where('UserNmae = ?',$username)
  $s->where('UserPW = ?',$pwd);

  $rowset = $this->fetchall($s);
  if(count($rowset) == 1)
  {
   return $row->current()->toArray();
  } else {
   return "Invalid"
  }
}

If you really want to generate the SQL by hand you can do so as well. Just use the query method of the Zend_Db_Adapter Object.

<?php
public function loginvalidation($username,$pwd)
{
  $rowset = $this->getAdapter()->query('UserName = \'' . $username . '\'and UserPW = \''. $pwd . '\'');
  if(count($rowset) == 1)
  {
   return $row->current()->toArray();
  } else {
   return "Invalid"
  }
}
Mike
What do you mean by the OP is *welcome to setup the application as you have*? The way it is setup now is not correct. Neither is it the way a TDG works, nor will PHP allow to redeclare property names in a class. In addition, the SQL you `query` from the DB adapter, is invalid. Also, it has to be `fetchAll` and method names should be camelCased according to ZF convention.
Gordon