2009年12月8日星期二

Re: Re: [fw-db] Howto run an SQL query directly against a database?

Thanks for the pointer!


/Darth apprentice




On Tue, Dec 8, 2009 at 11:36 AM, Johannes Schill <johannes80@polyester.se> wrote:
Hello,

The other one is when you return your result as objects. More about the different fetch modes:

Cheers,
Johannes


2009/12/8 W Itch <im.allergic.2.mailing.lists@gmail.com>

Thanks Gerlando that solved the remaining half of the problem. :-)
One last question if this is considered "fetchAll set to return the associated array" way.

    <a href="<?php echo $this->url(array('controller'=>'onemany', 'action'=>'edit', 'id'=>$album["id"]));?>">Edit</a>

Then what's the name for how this array works?  So I can google it and educate myself.

    <a href="<?php echo $this->url(array('controller'=>'onemany', 'action'=>'delete', 'id'=>$album->id));?>">Delete</a>



/Darth apprentice






On Tue, Dec 8, 2009 at 1:25 AM, <gerlando@gmail.com> wrote:
It looks like your fetchAll is set to return an associated array so you need to echo $album["title"];

-Gerlando.


On Dec 7, 2009 4:09pm, W Itch <im.allergic.2.mailing.lists@gmail.com> wrote:
> Thanks that helped me halfways towards the goal.  But I still need to bug you guys about the remaining half. :-)
>
> After some fiddling around it looks like I'm able grab the database values and send them to my View page as you can see in my screenshot.
>
> http://i50.tinypic.com/bfmfkg.jpg
>
> But I'm still not able to make the values display and List itself in the table.  What am I doing wrong here?
> index.phtml
>
> http://pastebin.com/m67919f07
>
>
>
>
> $albums = $db->fetchAll("SELECT * FROM albums");
>
> if(!$albums) echo "No records found!";
>
>  
> print_r($albums);
>
>  
>  
> echo "";
>
>         foreach($albums as $album)
>
>         {
>                 echo "";
>
>                 echo "" . $this->escape($album->title) . "";
>
>                 echo "" . $this->escape($album->artist) . "";
>
>                 echo "";
>
>         }
> echo "";
>
>  
> ?>

>  
>  
>
>  
> Default Akrabat code below, except for the escape parts. -->
>  
>
>
>
>     Title
>     Artist
>
>      
>
> foreach($albums as $album) : ?>
>
>
>     echo $this->escape($album->title);?>

>
>     echo $this->escape($album->artist);?>
>
>    
>         "url(array('controller'=>'onemany', 'action'=>'edit', 'id'=>$album->id));?>">Edita>

>
>         "url(array('controller'=>'onemany', 'action'=>'delete', 'id'=>$album->id));?>">Delete/a>
>
>    
>
> endforeach; ?>

>
>
>
>
>
>
> /Darth apprentice
>
>
>
>
>
>
> On Mon, Dec 7, 2009 at 6:57 PM, pablofmorales@gmail.com pablofmorales@gmail.com> wrote:
>
> You can try run Zend_Db with this example
>
> http://blog.pablo-morales.com/en/2009/12/cron-para-backup-optimizacion-tablas-php-zend-framework/
>
>
>
>
>
>
>
> ----------------------------------------
> Pablo Morales
> blog: http://blog.pablo-morales.com
> linkedln: http://www.linkedin.com/pub/9/528/21
>
>
>
> skype: pablofmorales
> gtalk: pablofmorales@gmail.com
> msn: pfm_mc@hotmail.com
>
>
>
>
>
>
> On Mon, Dec 7, 2009 at 2:52 PM, W Itch im.allergic.2.mailing.lists@gmail.com> wrote:
>
>
>
> Well before I started with the alterations, Zend_Log which for me required a database was logging all the CRUD operations correctly in a text file.
> And when I did this in the view page then I see all my database settings so it looks like I have database access.
>
>
>
>
>
>
> $db = Zend_Registry::get('db');
>
> Zend_Debug::dump($db);
>
>
> When reading the ZFiA book there was mention about the great war between developers who believes in Database abstraction/Table abstraction vs. those who believe in working with the specific SQL.  At first I choose the Database/Table abstraction way because the author uses it and there was a lot of security features mentioned.  But after spending about 6 months with this and trying to read the Zend Manual then I just feel more stupid the more I read.  I can't find any practical code examples that I can follow and test for myself.
>
>
>
>
>
> So now regardless of whether Dabase abstraction or pure SQL is the fastest and have the best performance.  For me it just boils down to just making it work.  I've been trying to ask for help for 6 months about how to do a certain 1-to-Many and Many-to-Many operation but I can't formulate my question to get an answer back.  If you weigh that against all the well written MySQL books out there then SQL to me seems like the easiest way.
>
>
>
>
>
>
>
> If I reformulate my question instead.  If I want to test this SQL code on my ZF website.  Which file can I paste it into for a quick and dirty test?  I plan to move database code to the Model folder later, but for quick and dirty tests where is the best place?
>
>
>
>
>
> $sql = 'SELECT * FROM albums';
> $result = $db->query($sql);
>
>
> And how can I make this code list all the results it has fetched from the database?
>
>
> /Darth apprentice
>
>
>
>
>
>
>
>
>
>
>
> On Mon, Dec 7, 2009 at 3:03 PM, pablofmorales@gmail.com pablofmorales@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
> You can not bring the model data directly from the view of the way that you're doing, is it the right way from a helper. Or directly from the action Controller
>
>
>
>
>
>
>
> You're sure that the registry keeps a copy of the default_adapter?
>
>
>
>
>
>
>
>
>
> Ideally, you use Zend_Db_Select This optimizes your querys for better performances
>
>
>
>
>
>
>
>
>
>
>
> ----------------------------------------
> Pablo Morales
> blog: http://blog.pablo-morales.com
> linkedln: http://www.linkedin.com/pub/9/528/21
>
>
>
>
>
>
> skype: pablofmorales
> gtalk: pablofmorales@gmail.com
> msn: pfm_mc@hotmail.com
>
>
>
>
>
>
> On Mon, Dec 7, 2009 at 10:38 AM, W Itch im.allergic.2.mailing.lists@gmail.com> wrote:
>
>
>
>
>
>
> Hi I've been studying Akrabat's Tutorial with the Albums collection for one year now approximately along with his book ZFiA.
> http://akrabat.com/zend-framework-tutorial/
>
>
>
>
>
>
>
>
> Everything works well and all that but I prefer to work with the MySQL language directly.  I find the Internet documentations and books about SQL easier to understand than how ZF does things with the Table abstraction thingy.
>
>
>
>
>
>
>
>
> So I'm trying to figure out how to fit things into my ZF MVC so I can begin coding in pure SQL while keeping some of the built-in ZF Database security thingys.
> Here are two code snippets of the relevant files I'm working on.  I'm trying to replace the old standard ZF list function with pure SQL code but everything breaks.
>
>
>
>
>
>
>
>
> $sql = 'SELECT * FROM albums';
>
> What am I doing wrong?
>
>
>
>
> class OnemanyController extends Zend_Controller_Action
>
>     public function indexAction()
>     {
>         $this->view->title = "One2Many (N:M) My Albums";
>
>         $this->view->headTitle($this->view->title, 'PREPEND');
>  
>         $albums = new Model_DbTable_Albums2();      
> // 0. This isn't necessary but I wanted to keep some of the old code before removing them completely.
>         //$this->view->albums = $albums->fetchAll();    //  1. ZF standard.
>         //$this->view->albums = $albums->listAlbum(); //  2. My custom SQL function which isn't in use.  I moved all the code to index.pthml for testing first.
>
>
>
>
>
>
>
>     }
>
>
>
>
> views/scripts/onemany/index.pthml
>
>
> url(array('controller'=>'onemany', 'action'=>'add'));?>">Add new album
>
>
>
>
>
>
>
>
>
> $db = Zend_Registry::get('db');
>
> $sql = 'SELECT * FROM albums';
> $stmt = $db->query($sql);
>
> $result = $stmt->fetchall();
>
> $albums = $result;
> ?>
>
>
>
>
>
>
>
>
>
>
>
>     Title
>     Artist
>     Genre
>      
>
>
>
>
>
>
>
>
>
>
>     escape($album->title);?>
>     escape($album->artist);?>
>    
>         url(array('controller'=>'onemany', 'action'=>'edit', 'id'=>$album->id));?>">Edita>

>
>
>
>
>
>
>
>         url(array('controller'=>'onemany', 'action'=>'delete', 'id'=>$album->id));?>">Delete/a>
>    
>
>
>
>
>
>
>
>
>
>
>
>
>
> /Darth apprentice


没有评论: