Dumb Question

View: New views
9 Messages — Rating Filter:   Alert me  

Dumb Question

by Joseph Crawford :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In my index.php bootstrap file I setup the database connection into a  
variable $db.  Now I need to access this in other controllers.  This  
seems like a dumb question to me but do I need to global $db; before I  
can access it in the indexAction method of the controller or is it in  
the global scope by default?

Thanks,
Joseph Crawford

Parent Message unknown Re: Dumb Question

by Joseph Crawford :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here is what I have

// User Controller

class UserController extends Zend_Controller_Action 
{
public function indexAction() {
        // Logic Code
        echo 'UserController';
        $user = new UserModel();
        var_dump($user);
        $row = $user->find(1);
        var_dump($row);
}

}

// User Model
require_once 'Zend/Db/Table/Abstract.php';

class UserModel extends Zend_Db_Table_Abstract {
/**
* The default table name 
*/
protected $_name = 'user';
protected $_primary = 'id';

}

However when i go to http://josephcrawford.dev/user/ All I see is the echo statement from the controller.

Parent Message unknown RE: Dumb Question

by rcastley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

RE: [fw-general] Dumb Question

 
The way I have done it (rightly or wrongly) and it works is:

Index.php
---------

$db = Zend_Db::factory('PDO_SQLITE', array('dbname' => 'myDb.db3'));
Zend_Db_Table_Abstract::setDefaultAdapter($db);

Then in my models directory I have

Users.php
---------

<?php
/**
 * Manage users table
 *
 */
class Users extends Zend_Db_Table_Abstract
{
    /**
     * Table name
     *
     * @var string
     */
    protected $_name = 'users';

    /**
     * Load current user data
     *
     * @param string $user
     */
    public function getUser($user)
    {
        $where = $this->getAdapter()->quoteInto('username = ?', $user);

        return $this->fetchRow($where);
    }
}

Then in my controller I have

IndexController.php
-------------------

$userTable = new Users();

$getUser = $user->getUser($currentUsername);

$this->view->name = $getUser->name;
$this->view->age = $getUser->age;
Etc......


Hope this helps.

- Robert

-----Original Message-----
From: Joseph Crawford [codebowl@...]
Sent: 15 May 2008 14:17
To: fw-general@...
Subject: [fw-general] Dumb Question

In my index.php bootstrap file I setup the database connection into a variable $db.  Now I need to access this in other controllers.  This seems like a dumb question to me but do I need to global $db; before I can access it in the indexAction method of the controller or is it in the global scope by default?

Thanks,
Joseph Crawford

________________________________________________________________________
This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system.

________________________________________________________________________


________________________________________________________________________
This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system.
________________________________________________________________________

RE: Dumb Question

by Fullarton, Daniel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

RE: [fw-general] Dumb Question

AFAIK It is not global by default, you could probably find a few neater ways to do this but I just use

Zend_Db_Table_Abstract::setDefaultAdapter($db);

(this also sets up for any Zend DB Tables you may/may not have) and then use

Zend_Db_Table_Abstract::getDefaultAdapter();

to get it from your controllers.

I'm sure there must be a lighter way to do it without requiring that static method.

- Daniel Fullarton



-----Original Message-----
From: Joseph Crawford [codebowl@...]
Sent: Thu 5/15/2008 11:16 PM
To: fw-general@...
Subject: [fw-general] Dumb Question

In my index.php bootstrap file I setup the database connection into a 
variable $db.  Now I need to access this in other controllers.  This 
seems like a dumb question to me but do I need to global $db; before I 
can access it in the indexAction method of the controller or is it in 
the global scope by default?

Thanks,
Joseph Crawford


Re: Dumb Question

by Chris Tankersley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You can always just set it in Zend_Registry too. In my bootstrap, I save
it to the registry after I make my $db object so that I have access to
it on the off-chance my Controllers need to run a direct DB query.

Chris

Joseph Crawford wrote:
> In my index.php bootstrap file I setup the database connection into a
> variable $db.  Now I need to access this in other controllers.  This
> seems like a dumb question to me but do I need to global $db; before I
> can access it in the indexAction method of the controller or is it in
> the global scope by default?
>
> Thanks,
> Joseph Crawford
>


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


Re: Dumb Question

by Jim Scherer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

if you are using Zend_Controller_Front as I am you can set it as a parameter:

$frontController = Zend_Controller_Front::getInstance();
$frontController->setParam('db', $db)

and then in your controller:

$this->getFrontController()->getParam('db')

So many ways to skin a cat.

Jim

Joseph Crawford wrote:
In my index.php bootstrap file I setup the database connection into a  
variable $db.  Now I need to access this in other controllers.  This  
seems like a dumb question to me but do I need to global $db; before I  
can access it in the indexAction method of the controller or is it in  
the global scope by default?

Thanks,
Joseph Crawford

RE: Dumb Question

by Kevin Hallmark :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I usually access the database through my table objects.

However, recently I've needed to do things directly with the database
object. I found the simplest choice was $GLOBALS['db'] = $db.

It's so rare that I directly use it, and a global lookup is pretty cheap
(right?). Anytime I need an object instantiated in my bootstrap I'll
assign it to a global. If I access it more than once in a function, I'll
either localize the variable or try and get it a more reliable way.

I didn't think about using:
Zend_Db_Table_Abstract::getDefaultAdapter();

That seems like it might be a good alternative choice.

Kevin Hallmark
PHP Developer
Bonnier Corporation


-----Original Message-----
From: Jim Scherer [mailto:jscherer26@...]
Sent: Thursday, May 15, 2008 10:12 AM
To: fw-general@...
Subject: Re: [fw-general] Dumb Question


if you are using Zend_Controller_Front as I am you can set it as a
parameter:

$frontController = Zend_Controller_Front::getInstance();
$frontController->setParam('db', $db)

and then in your controller:

$this->getFrontController()->getParam('db')

So many ways to skin a cat.

Jim


Joseph Crawford wrote:
>
> In my index.php bootstrap file I setup the database connection into a

> variable $db.  Now I need to access this in other controllers.  This  
> seems like a dumb question to me but do I need to global $db; before I

> can access it in the indexAction method of the controller or is it in

> the global scope by default?
>
> Thanks,
> Joseph Crawford
>
>

--
View this message in context:
http://www.nabble.com/Dumb-Question-tp17253025p17254165.html
Sent from the Zend Framework mailing list archive at Nabble.com.


Re: Dumb Question

by David Mintz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I create a $db in my bootstrap and save a reference in Zend_Registry. I use getDefaultAdapter() whenever possible but I have some models with static methods, which by definition can't reference $this, so they pull it from the registry.

On Thu, May 15, 2008 at 11:10 AM, Kevin Hallmark <Kevin.Hallmark@...> wrote:
I usually access the database through my table objects.

However, recently I've needed to do things directly with the database
object. I found the simplest choice was $GLOBALS['db'] = $db.

It's so rare that I directly use it, and a global lookup is pretty cheap
(right?). Anytime I need an object instantiated in my bootstrap I'll
assign it to a global. If I access it more than once in a function, I'll
either localize the variable or try and get it a more reliable way.

I didn't think about using:
Zend_Db_Table_Abstract::getDefaultAdapter();

That seems like it might be a good alternative choice.

Kevin Hallmark
PHP Developer
Bonnier Corporation
<snip/>

--
David Mintz
http://davidmintz.org/

The subtle source is clear and bright
The tributary streams flow through the darkness

Re: Dumb Question

by Pádraic Brady :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Joseph,

What I usually do is three fold:

1. Add is as the default adapter to Zend_Db_Table for use by all my Zend based Models. This also acts as a Registry by the way, since you can call Zend_Db_Table::getDefaultAdapter() anywhere.

2. Add it to a Registry as would be normal practice.

3. Possibly add it as a Front Controller parameter so you can retrieve it from any controller action using that method.

Where possible I prefer 3, simply because I prefer to avoid as many static calls in my controllers as possible. But a Registry works just fine so long as you use it only as needed.

Paddy

Joseph Crawford wrote:
In my index.php bootstrap file I setup the database connection into a  
variable $db.  Now I need to access this in other controllers.  This  
seems like a dumb question to me but do I need to global $db; before I  
can access it in the indexAction method of the controller or is it in  
the global scope by default?

Thanks,
Joseph Crawford
Pádraic Brady

http://blog.astrumfutura.com
http://www.patternsforphp.com
OpenID Europe Foundation - Irish Representative