Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

find('list') only gives one column output only in MongoDB #667

Closed
nilamdoc opened this Issue Oct 22, 2012 · 6 comments

Comments

Projects
None yet
3 participants

I want to create a dropdown select using the data from a table/collection for user input.

in my view:
echo $this->form->select('vol_types',$types);

I have a find on the table or collection 'types'

in my controller:
$types = Types::find('list',array(
'fields'=>array('name','id'),
'order'=>'id'));
return compact('types');

it is only outputting one column in the array output in MongoDB.

If I execute this with MySQL Connection, it will output a correct array.

Array
(
[1] => JACKET CLUSTER FRONT
[2] => TITLE WITHOUT SYMBOL
[3] => FRONTISPIECE

)

When I execute it with MongoDB connections it only gives the _id and not the name field associated with the id.
This is the result of the array:

Array
(
[5082c6109d5d0c640c000000] => MongoId Object
(
[$id] => 5082c6109d5d0c640c000000
)

[5082c62b9d5d0c440c00006e] => MongoId Object
    (
        [$id] => 5082c62b9d5d0c440c00006e
    )

[5082c62b9d5d0c440c00006f] => MongoId Object
    (
        [$id] => 5082c62b9d5d0c440c00006f
    )

)

Owner

nateabele commented Oct 23, 2012

You can't use the exact same query with both MySQL and MongoDB, because, for one thing, they have different primary keys. Try 'fields' => array('_id', 'name').

@nateabele nateabele closed this Oct 23, 2012

Thanks for the reply,
I have tried using 'fields' => array('_id', 'name') and 'fields' => array('id', 'name') for MongoDB.

My MongoDB structure is:
{
"_id": ObjectId("5082c62b9d5d0c440c000080"),
"id": "2",
"name": "BLANK PAGES"
}

In both the cases
$types = Types::find('list',array(
'fields'=>array('name','id'),
'order'=>'id'));

or
$types = Types::find('list',array(
'fields'=>array('name','_id'),
'order'=>'_id'));

it returns the same

  [5082c62b9d5d0c440c00006f] => MongoId Object
    (
     [$id] => 5082c62b9d5d0c440c00006f
    )

it never returns the field 'id'.

'fields'=>array('id','name') it returns '_id'
'fields'=>array('_id','name') it also returns '_id'

I have uploaded the test environment for MongoDB and MySQL both on https://github.com/nilamdoc/li3_test

The following are the code in brief after downloading the lithium framework.

connections.php: using 2 connections both with unique names.
Connections::add('Mongo', array(
'type' => 'MongoDb',
'host' => 'localhost',
'database' => 'li3_test'
));

Connections::add('MySQL', array(
 'type' => 'database',
 'adapter' => 'MySql',
 'host' => 'localhost',
 'login' => 'root',
 'password' => 'hitarth',
 'database' => 'li3_test',
 'encoding' => 'UTF-8'
));

Mongo has a database (li3_test) and collection(mongo_types) with 4 records as per the below screen shot.

MySQL has a database(li3_test) and table(my_types) with 4 records as per the below screen shot.

Two Models are created independently:

  1. MongoTypes.php
namespace app\models;

class MongoTypes extends \lithium\data\Model {
    public $_meta = array('connection' => 'Mongo');
    public $validates = array();
}

?>
  1. MyTypes.php 'MySQL'); public $validates = array(); ``` } ?>

Two controllers are created independently:
MongoTypesController.php

public function index() {
    $mongoTypes = MongoTypes::all();

    $MongoTypesSelect = MongoTypes::find('list',array("fields"=>array("id",'name')));
    $MongoTypesSelectID = MongoTypes::find('list',array("fields"=>array("_id",'name')));        
    $MongoTypesSelectName = MongoTypes::find('list',array("fields"=>array('name')));                
    return compact('mongoTypes','MongoTypesSelect','MongoTypesSelectID','MongoTypesSelectName');    }

MyTypesController.php
public function index() {
$myTypes = MyTypes::all();
$MyTypesSelect = MyTypes::find('list',array("fields"=>array("id",'name')));
return compact('myTypes','MyTypesSelect');
}

Two views are created independently:
views/mongo_types/index.html.php
<?
echo $this->form->create();

echo '

find("list",array("fields"=>array("id","name"))
';
echo $this->form->select('vol_types',$MongoTypesSelect);
echo '
find("list",array("fields"=>array("_id","name"))
';
echo $this->form->select('vol_types',$MongoTypesSelectID);
echo '
find("list",array("fields"=>array("name"))
';
echo $this->form->select('vol_types',$MongoTypesSelectName);

echo $this->form->submit('Submit');
echo $this->form->end();
?>

views/my_types/index.html.php
form->create(); echo $this->form->select('vol_types',$MyTypesSelect); echo $this->form->submit('Submit'); echo $this->form->end(); ?>

I am sure you should be able to use this independent environment to test.

@nateabele nateabele reopened this Oct 24, 2012

Hi Nate,

I further investigated and found that, one needs to declare $_meta and $_schema as under:

class MongoTypes extends \lithium\data\Model {
public $_meta = array('connection' => 'Mongo',array(
'key' => '_id',
'locked' => true
));
public $validates = array();

protected $_schema = array(
    '_id'   =>  array('type' => 'id'),
    'name'  =>  array('type' => 'string', 'null' => false),
);

to get the correct results.

We will need to add a section in the Docs for MongoDB for new users.

the solution for this is quite easy: just declare
protected $_meta = array(
'title' => 'title',
);
where title is your field you which to act as title.

Owner

nateabele commented Nov 20, 2012

Yes, this. Thanks @mariuskubilius.

@nateabele nateabele closed this Nov 20, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment