Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)

[eluser]KSiimson[/eluser]
I got this code. AFAIK it should print out all of the country names from the country table, but it only does one.

Inventory and country models have a many to many relationship defined.

Code:
$inventory = new inventory;
$country = new country;
foreach($country->get()->all as $c)
{
echo "<li>".$c->printable_name."</li>";
}

Profiler tells this:
Quote:0.0003 SELECT * FROM `countries` LIMIT 1
0.0001 SELECT COUNT(*) AS `numrows`
FROM (`hub_countries`)

0.0004 SELECT * FROM `inventory` LIMIT 1
0.0003 SELECT * FROM `countries` LIMIT 1
0.0001 SELECT COUNT(*) AS `numrows`
FROM (`countries`)
0.0002 SELECT * FROM `inventory_projects` LIMIT 1

How do I get an array with all country names from the table? I need to print them for a form.

[eluser]OverZealous[/eluser]
The code you copied in and the query output do not match. There is no way $country->get() in a stock DMZ would create the sample of SQL you provided.

You can ignore all of the SELECT * FROM xxx LIMIT 1 queries — those are initialization queries.

The COUNT(*) queries tell me you are using ->count() instead of ->get().

[eluser]KSiimson[/eluser]
There is a count() in the code, I only copied the relevant part.

This is produced by get():
Quote:0.0001 SELECT COUNT(*) AS `numrows`
FROM (`countries`)

edit:
Perhaps this gives a better overview of what is going on.
Code:
$inventory = new inventory;
            
            $country = new country;
            $country->get();
            $countries = $country->all;
                    
            echo "Total: " . $country->count();
            
            $i = 0;
            foreach($countries as $c)
            {
                $i++;
            }
            
            echo "Total PHP: " . $i;

Quote:0.0008 SELECT * FROM `inventory` LIMIT 1
0.0005 SELECT * FROM `countries` LIMIT 1
0.0010 SELECT *
FROM (`countries`)
0.0003 SELECT COUNT(*) AS `numrows`
FROM (`countries`)

That would produce:
Quote:Total: 240
Total PHP: 1

I would expect it to result "Total PHP: 240".

Thanks

[eluser]bEz[/eluser]
I believe $countries is assigned by reference and not it's own copy of the "country->all" object. (Phil, feel free to correct me on this)

Try the below changes:
Code:
$inventory = new inventory;
$country = new country;

$country->get();
// ============================
// Comment out this line and place below
// $countries = $country->all;
// ============================                    
echo "Total: " . $country->count();


// ============================
// re-get() the data and then apply the "all" method.
$country->get();
$countries = $country->all;            
// ============================

$i = 0;
foreach($countries as $c)
{
  $i++;
}
echo "Total PHP: " . $i;
Aside from testing purposes, the above code changes should provide like numbers.

I believe, if you also used the "array" extensions. you coud do the following (untested of course).

Code:
$country = new country;
$country->load_extension('array');
$country->get();
$c_cnt = count( $country->all_to_array() );

EDIT: Above Tested.

IF all you want is a count, then the "extension" version is overkill.
IF you wanted the array, but wanted the count as well, then the extension method is the better choice.
IF you are trying to test "PHP" vs "PHP via CI" then I think you maybe fighting a useless battle.

[eluser]KSiimson[/eluser]
Silly question, but on first comment, should I comment out the line above or below the comment?

(The count is there just for testing)

[eluser]bEz[/eluser]
The first comment shows where you had "$countries = $country->all;"

The second comment shows where to place it.

but I just setup a method using your original code, and it worked...
So check your model setup.
Code:
function test()
  {
    $pt = new Pool_team();
    $pt->get();
    $pteams = $pt->all;
    echo $pt->count();
    $i=0;
    foreach ($pteams as $p)
    {
      $i++;
    }
    echo br().$i;
  }

both gave me the same numbers.
but try the version using the extensions, it's more cleaner and along the lines of what you may be needing in your project.

[eluser]Jinkusu[/eluser]
[quote author="OverZealous" date="1254183259"]

You will almost certainly have trouble with deletions and possibly even updates if you do not remove that column!
[/quote]

The weird thing is that field has nothing to do with the DB id, its suppose to be a automatically generated id number (like on your ID card at College). I've already changed it to student_number.

[eluser]KSiimson[/eluser]
country.php
Code:
&lt;?php

/*
* Country Class
*/

class Country extends DataMapper
{
    var $table = "countries";
    var $has_many = array("inventory");
}

function Country()
{
    parent::DataMapper();
}

/* End of file country.php */
/* Location: ./application/models/country.php */

?&gt;

inventory.php
Code:
&lt;?php
class Inventory extends DataMapper
{
    var $table = "inventory";
    var $has_many = array('country');

    /**
     * Constructor
     *
     * Initialize DataMapper.
     */
    function Inventory()
    {
        parent::DataMapper();
    }

    // --------------------------------------------------------------------

    /**
     * Prepare For Query
     *
     * Prepares the necessary pre-query conditions for this object.
     *
     * @access    private
     * @return    void
     */
    function _prepare_for_query()
    {
        // Overload this in your inheriting models
    }

    // --------------------------------------------------------------------


    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *                                                                   *
     * Overload methods                                                  *
     *                                                                   *
     * The following are methods that overload the default               *
     * functionality of DataMapper.                                      *
     *                                                                   *
     * It is necessary for self referencing relationships.               *
     *                                                                   *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


    // --------------------------------------------------------------------

    /**
     * Get (overload)
     *
     * Get objects.
     *
     * @access    public
     * @param    int or array
     * @return    bool
     */
    function get($limit = NULL, $offset = NULL)
    {
        $this->_prepare_for_query();

        return parent::get($limit, $offset);
    }

    // --------------------------------------------------------------------

    /**
     * Count (overload)
     *
     * Returns the total count of the objects records.
     * If on a related object, returns the total count of related objects records.
     *
     * @access    public
     * @param    int or array
     * @return    bool
     */
    function count()
    {
        if (empty($this->parent))
        {
            $this->_prepare_for_query();
        }

        return parent::count();
    }
}
    
/* End of file inventory.php */
/* Location: ./application/models/inventory.php */

I think it should all be correct, and I can't figure out why this is not working.

[eluser]bEz[/eluser]
I see you are prepared to OVERLOAD the get() and count() methods.

Try commenting those out in the model file and retry the controller test.

[eluser]KSiimson[/eluser]
Great, thanks for pointing that out. It was a copy-paste from documentation and I wasn't sure what that part was there for. Unfortunately, it still gives the same result. Sad




Theme © iAndrew 2016 - Forum software by © MyBB