CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 06-23-2009

[eluser]gshipley[/eluser]
[quote author="OverZealous.com" date="1245802469"]@gshipley

This is how you get what you want:
Code:
// generates a query like (excluding the joins):
// SELECT * FROM usermovies ORDER_BY last_watched LIMIT 1
$tmpUser->usermovie->order_by('last_watched', 'DESC')->get(1);

Now $tmpUser->usermovie will contain a single usermovie with the maximum last_watched value.

Understand this, however: As far as the database knows, there could be more than one movie with the same maximum "last_watched" (apparently, you are using dates here, so it should not happen). That's why select_max cannot return a single usermovie. The example I give above only works consistently for columns where there can only be one max value in a column.[/quote]

Perfect, thanks!


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]PoetaWD[/eluser]
Can you help me with this error ?

Code:
Fatal error: Class 'tiposvendum' not found in C:\xampp\htdocs\ecos\system\application\libraries\datamapper.php on line 3005

Model: tiposvenda.php

Code:
<?php
class Tiposvenda extends DataMapper {
    
    function Tiposvenda()
    {
        parent::DataMapper();
    }
}
?>

Database:

Code:
CREATE TABLE IF NOT EXISTS `tiposvendas` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `stNome` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Controller:

Code:
$obj = new Tiposvenda();
            $obj->get();
            
            $data['objects'] = $obj;
            

                    
            $this->load->view('tipo_venda/lista', $data);

Also, what is the better way of sending the result of all itens to the view file ?

Like, I want to build a html table with all the objets of the type 'Tipovenda'...


Thank you


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]PoetaWD[/eluser]
Just found that the error was because the helper was changing the names to plural... but the second question stays...

I did like this:

Code:
$obj = new Tiposvenda();
            $obj->get();
            
            foreach ($obj->all as $objt)
            {
                $data['objetos'][$obj->id]['stNome'] = $obj->stNome;
            }
                    
            $this->load->view('tipo_venda/lista', $data);

But is there a better way ? Because I will have to do a second loop in the view file... and might be a better, quiker way to do it...

Thank you !


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]tdktank59[/eluser]
That's basically the fastest way that I know...

You could do $data['objects'] = $obj->all; but that doesn't do much since you have to do it anyways.
Otherwise that's the best you can do... (To my knowledge)


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]PoetaWD[/eluser]
[quote author="tdktank59" date="1245893174"]That's basically the fastest way that I know...

You could do $data['objects'] = $obj->all; but that doesn't do much since you have to do it anyways.
Otherwise that's the best you can do... (To my knowledge)[/quote]

Just updated my post...

But... how to write the information in the view file ? How would the loop be ?

Like this ?

Code:
foreach ($object as $obj)
{
    echo $obj->title
}

I am sorry if those are newby questions... I am really trying to learn php.. hehe

Thank you


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]tdktank59[/eluser]
Ah lets see if I can help with this!

First off in the controller
Code:
$obj = new Tiposvenda();
$obj->get();

$data['objetos'] = $obj->all; // or $obj in the view you can add the ->all    
                    
$this->load->view('tipo_venda/lista', $data);

Now in the view
Code:
foreach ( $objetos as $o ) // or $objetos->all if you passed $obj in the controller
{
    echo $o->stNome.'<br />';
}

That would print out

Quote:$stNome
$stNome
$stNome
$stNome

where $stNome is the value from the database for each row.

Hope that makes sense


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]PoetaWD[/eluser]
[quote author="tdktank59" date="1245894125"]Ah lets see if I can help with this!

First off in the controller
Code:
$obj = new Tiposvenda();
$obj->get();

$data['objetos'] = $obj->all; // or $obj in the view you can add the ->all    
                    
$this->load->view('tipo_venda/lista', $data);

Now in the view
Code:
foreach ( $objetos as $o ) // or $objetos->all if you passed $obj in the controller
{
    echo $o->stNome.'<br />';
}

That would print out

Quote:$stNome
$stNome
$stNome
$stNome

where $stNome is the value from the database for each row.

Hope that makes sense[/quote]

It sure does !

Thank you !

If this is the best way of doing... i will do it...

Thank you very much !


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]tdktank59[/eluser]
Not sure if its the best way but its the best way I know how to do it lol...
And the only way I know how to...

Of course there are a few other ways like taking what you want and processing it into anther array before passing to the view... But at some point its why?


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]OverZealous[/eluser]
Just to throw in my $0.02:

I think that tdktank59's example is the best way to handle it. It has the least amount of duplication (copying the data into another array would eat up CPU and RAM, with no benefit), and makes the most sense MVC-wise: the model is the $objetos, which is what is passed between the View and the Controller.

Personally, I waffle between passing in $object and $object->all. Both make sense for different reasons. The nice thing about passing in $object->all directly, is you might reduce the risk of accidentally attempting to loop over $object (which I have done many, many times).


DataMapper 1.6.0 - El Forum - 06-24-2009

[eluser]tdktank59[/eluser]
And looping $object is quite large lol..

Only thing id say is keep it consistent throughout your site so you don't confuse yourself.
I like using the ->all in the controller since then im only passing the data. And can use it right away. Unless of course its a larger array containing multiple rows of data in which case I loop it..

do this to see what looping $object can do lol...
Code:
echo '<pre>'.print_r($objetos).'</pre>';
its quite large lol...