Defining a function to a Row - zend-framework

im making an application and i need to implement diferent level of user permits.
I coul have the function
$this->view->users->hasPermits($this->view->user);
By declaring a function on the model, an things could be easy to implement. But i would like to be able to have the next function doing the same:
$this->view->user->hasPermits();
Is there a way to do this? do i need to extend the Zend_Db_Table_Row class? any ideas?

Use Zend_Acl for this.
There is a Zend ACL tutorial on YouTube.

If $user is a Zend_Db_Table_Row from the Zend_Db_Table $users, than in User you could perform the method like so:
public function hasPermits()
{
return $this->getTable()->hasPermits($this);
}

Related

how to call custom php function in zend controller?

How can i call simple custom php function in zend controller ?
class Ozone_IndexController extends Zend_Controller_Action
{
public function snetworkAction(){
}
function curl_file_get_contents($url){
}
}
how i can call curl_file_get_contents($url) function inside snetworkAction
Your question is definitely ZF2-agnostic. It really boils down to OOP basics.
Have you set up your route for snetworkAction? If so, then the contents of your action, to call that other object method, would simply be:
public function snetworkAction(){
$this->curl_file_get_contents( 'http://someurl.com' );
}
private function curl_file_get_contents( $url ){
...
}
I would strongly recommend getting a grasp on basics before going deep into ZF2. It's really going to be a frustrating experience if you don't have the fundamentals down pat. It's a very unique framework, and while excellent, will cause neophytes to bleed!
Good luck man.
you can call curl_file_get_contents($url) like
$this->curl_file_get_contents($url);
it is better to define function as protected

Zend Model access in singleton class - best approach

I'm looking for best pattern/approach to access one table data in singleton class (in ZF 1.x). In details:
I have one singleton class (just like Zend_Date for example) that make for me some basic abstract stuff very detached from application reality.
In this class, in two points, I need to access to one db table and I need to make some basic operation on it.
It's not a problem to use my regular ZF models class inside functions of this singleton. It works fine. Now it look like:
class My_ZF_Singleton
{
...
public function someFunctionInMySingleton()
{
...
$oModel = new Model_My_Model_Form_ZF_Application();
$oModel->letsDoSomeStuffWithDb();
...
}
...
}
But I feel in my bones that it's not a very good solution, not so glamour as I would like to be. It make my singleton class more attached to application then it should be. I would like to use some other pattern to access this db data then application model class. I would be very thankfull for any clue or better solution - it's not a "hey I'm stuck probem" or "hey I've got an error" - I'm just looking for better solution.
Not sure I quite understand your question or want the point might be, but I'll try.
In ZF1 the database adapter is typically a singleton already. Multiple databases maybe connected to but each will require a unique identification. Typical access to the default adapter setup in the application.ini or Bootstrap.php:
$adapter = Zend_Db_Table::getDefaultAdapter();
a common way to provide access to a single database table and give access to the Zend_Db_Table api is to build a DbTable model:
class Application_Model_DbTable_TableName extends Zend_DbTable_Abstract
{
protected $_name = 'Table_Name' //required if classname does not match table name
protected $_primary = 'primary_key_column_name'//optional, use if primary key is not 'id'
}
You can treat this class as an instance of the default database adapter for a single table (works really well in a mapper). You can also add functions to this class to override or add to the default Zend_Db_Table api.
I hope this at least comes close.

How to define own functions in zend framework

Actually I wanted to know that is there any way to define and call a function in Zend framework which does not need to put with $this like pr() function in cake php. I want to define a such a function which can be called directly in all controllers and views in zend framework. Can it be done by putting all functions in helper and use them without writing $this in front of them.
For example I wanted to make a function arrprint() to print the array in helper and use it globally with only by writing simply arrprint();
Please help me
You could just create a file anywhere and put the function in there. The just include it like you would any other files.
Or create a library called Utils and put your custom functions there as static and call them using:
Utils::myFunction();
Contrary to cake zf does not enforce much anything. You can leverage the framework to do whatever you want.
BTW $this is just a pointer to the current class and when you use $this->myFunction(); it's because that function is a member of the current class (or its parent).

How do i use findAll method with a class in Doctrine?

I am a little bit confused with Doctrine class. I created a class and its base class in Doctrine. The class's name is User.
so..I created an object of class User.
$oUser = new User();
when I try to use the findAll method, it does not work. I found the following code on the doctrine documentation:
Doctrine_Core::getTable('User')->findAll();
I don't understand why i need to call getTable to use the findAll method when I have User class.
Am I missing something?
For Doctrine 2, you need to get the repository:
$AllUsers = EM()->getRepository('Users')->findAll();
AFAIK User object represents a single row in a table.if you need all the users you need to ask the table.

Can I set the Database adapter to use permanently from within a Zend_Db_Table_Abstract Class?

I have 2 databases that my site uses including a central user database that relates to other site-specific databases.
Sometimes it is adequate to call new User(array('db'=>'adapter1')); (but never convenient); other times, though, such as when declaring relationships between tables on different databases, there is no way to do this.
Does anyone know a way to specify which database adapter to use from within the Zend_Db_Table_Abstract class?
Thanks!
Getting back to this pretty late, but none of the answers here quite did it for me. A select few of my database models needed to use 'tdb' and the following code was added to each of those classes to have that happen automatically:
protected function _setupDatabaseAdapter()
{
$this->_db = Zend_Registry::get('tdb');
parent::_setupDatabaseAdapter();
}
I thank you all for your suggestions along the way!
Zend_Db_Table_Abstract provides a static method to set the default database adapter. Do this as follows:
Zend_Db_Table_Abstract::setDefaultAdapter($adapter);
Now, all your Table objects will use your adapter by default.
Note: the online docs sometimes don't make this obvious, so your second best place to check is in the API here: http://framework.zend.com/apidoc/core/
You could set the class variable $_db to the correct adapter in the constructor.
global $adapter1; //There are better ways than using a global variable
$this->_db = $adapter1;
Assuming the adapter object can be referenced in the constructor. That doesn't seem to portable, but I believe it would work.
The init function can be used, it is not used in Zend_Db_Adapter_Abstract, can be used in your class to setup whatever needs to be done. _setAdapter accepts a string naming a Registry Key.
public function init()
{
$this->_setAdapter('tdb');
}