How to change the database field orders in zend - zend-framework

In zend project I fetch data from the database and show in a table. It shows data in the same order as stored in the database. But I want to show the fileds in diffeent order than in database order. But I don't know how to do thid. Here I means field order not the row order.
Please help me in this regard.
Thanks

Its hard to say how youre displaying but lets assume you have made Table Classes for each table i would do something like this:
<?php foreach $zendDbRowObject->getTable()->getDisplayOrder() as $fieldName): ?>
<?php echo $zendDbRowObject->$fieldName; ?>
<?php endforeach; ?>
So then in your Table clas for a particular table you can create a property/method to get the fields in the order you want them in:
public function getDisplayOrder() {
// fake column names obviously... use yours here.
return array(
'column5',
'column1',
'column4',
'column2',
'column3'
);
}

when you do db select, you can select the coloumns that you want, so instead of select *,
you do select col5,col3,col2,col6,col1 from tablename
This will change the order of the columns
$select = $db->select()
->from(array('t' => 'table'),
array('t.col2', 'p.col1`'));

Related

how to select particular column from relationship collection table in laravel mongodb jessenger

I have 3 columns in my database. 2 columns is connected with one column by using a hybrid relationship.
here is my query.
$data=Client::with('product','department')->select(['product.product_name','product.product_description']);
how to select row from another table?
in your product or department relation method do the selection with all the forging keys if you have any other relation for product for later use is you want them like
public function product()
{
return $this->hasMany(department::class)->select(['id', 'another_relation_to_product_id', 'product_name', 'product_description']);
}
You can do it this way
$data = Client::with(['product:id,product_name,product_description','department'])->get();
see docs https://laravel.com/docs/5.7/eloquent-relationships#constraining-eager-loads in Eager Loading Specific Columns section. Or you can do it
App\User::with([
'product' => function ($query) {
$query->select('id', 'product_name', 'product_description');
},
'department'
])->get();

columns of joined table missing

I'm missing the columns of a joined table. I have two tables like:
t_q_filialen 1 --> n t_mandant
I did my select statement in my model as follows:
$select= new Select (); //another try ('t_mandant','t_q_filialen');
$select->columns(['id', 'cccid' ,'geschid', 'name', 'langname','filiale']);
$select->from('t_mandant');
$select->join('t_q_filialen', 't_q_filialen.id=t_mandant.geschid ', [ 'filialeid'=>'id','filiale'=>'name']);
I expected to get the column name with the given alias 'filiale' like this in my view script:
$this->escapeHtml($mandant->filiale);
I get an error.
Unknown column 't_mandant.filiale' in 'field list'
If I change back to 'geschid' which is the foreign key-column in the table t_mandant and try:
var_dump(get_object_vars($mandant));
the columns from the table 't_q_filialen' are not in the collection.
What's wrong?
I have a small idea that I could be something with paginator. I instantiate paginator like this:
$resultSetprototype=new ResultSet();
$resultSetprototype->setArrayObjectPrototype(new Mandant());
$paginatorAdapter= new DbSelect($select, $this->tableGateway->getAdapter(), $resultSetprototype);
$paginator=new Paginator($paginatorAdapter);
return $paginator;
How can I change this, so that my join will be accepted? Might have something to do with this:
$resultSetprototype->setArrayObjectPrototype(new Mandant());
In other models without pagination I use this one and it works:
$rowset = $this->tableGateway->adapter->query($statment, "execute");
So which could be the solution? I really would prefer the last possibility, but I have no idea how to use it in combination with paginator.
Any help appreciated!
I listed several ideas, but my goal would be, to have the columns of both tables in my collection with the paginator-object.
oh my god, I just tried:
$paginatorAdapter= new DbSelect($select, $this->tableGateway->getAdapter());
Without the resultsetprototype

Doctrine 2 + Zend Form - Populate Dynamic Select Menus

I'm building a Zend form that has a dropdown/select menu populated with data from a Doctrine 2 query.
In my repository class, I have the following query in a method named selectUser():
$query = $em->createQuery('SELECT u.id, u.name FROM XX\Entity\Users u ORDER BY u.name ASC');
$users = $query->getResult();
This returns a multidimensional array, which I'm trying to loop through like this (within the same method):
$options = array();
foreach ($users as $key => $value) {
$options[$value['id']] = $value['name'];
}
return $options;
Then in my Zend form class, I try to populate the Select element like this:
$id = new Zend_Form_Element_Select('id');
$options = $this->usersRepository->selectUser();
$id->AddMultiOptions($options);
The result is an error for each user row that states "Undefined index: [name] in ...UsersRepository.php..." where [name] is the value of the 'name' column in each row.
Does anyone see what I'm doing wrong or how to populate a dynamic select menu using Doctrine 2 and Zend Framework?
(By the way, in order to run the repository method, the form class has protected properties representing the Doctrine container, entity manager, and Users repository. If this isn't considered best practice, I'd welcome any suggestions on improving my technique.)
I think your problem is here
$options[$value['id'] = $value['name']];
this would be better
$options[$value['id']] = $value['name'];

Doctrine join query access problem

i have two tables pages and page_desc and have a relation
$this->hasMany('Model_PagesDesc as PageDesc', array(
'local' => 'id',
'foreign' => 'pages_id'));
i have a query
return Doctrine_Query::create()
->select('m.*, d.*')
->from('Model_Pages m')
->leftJoin('m.PageDesc d')
->execute();
NOW WHEN QUERY EXECUTES. iN VIEW WHEN I TRY TO GET FIELD VALUE OF SCEOND TABLE IT RETURNS SOME INTEGER INSTEAD OF ACUAL VALUE IN FIELD
<?php echo $pages->PageDesc->content;?>
If youre using hasMany then you should Model_Pages::PagesDesc will be a Doctrine_Collection not a Model_PagesDesc instance. Im not sure but i would assume that the default behavior of the collection is to return the count of elements in the collection when converted to string, thus the integer. You need to either loop over the collection or get a specific item.
<?php echo $pages->PageDesc->getFirst()->content; ?>
OR
<?php foreach($pages->PageDesc as $desc): ?>
<?php echo $desc->content; ?>
<?php endforeach; ?>
Or you could write a custom collection for this model that returns concat of the content fields for every element in the collection.
However it seemdsthat a page shouldnt really have more than one description should it? You might want to consider using a 1-1 relation or just making the description a column on your page model.

Order Zend_Db_Table rowset by reference column

i know i can define relationships through _referenceMap, i know that i con join selects trough
$db->select()
But what i need is to fetch rowset in model extending Zend_Db_Table_Abstract and then order it by value of referenced column from another table.
Is there some workaround to do that?
edit:
heres is the example:
first table:
table bugs columns id, bugname, authorid
second table:
table authors columns id, authorname
I have a model Model_Bugs extends Zend_Db_Table_Abstract
I want to make something like this:
$model->fetchAll($model->select()->order('authorname ASC'))
This means, that i need to join tables and sort by a column, which is not in the model table.
thanks for help
Jan
I would add a method in Model_Bugs like so:
public function fetchBugsByAuthorname() {
$bugTable = $this;
$bugTableName = $this->info('name');
$authorsTable = new Model_Authors();
$authorsTableName = $authorsTable->info('name');
$select = $bugTable->select()
->setIntegrityCheck(false)
->from($bugTable, array('id', 'bugname', 'authorid'))
->join($authorsTableName,
"$bugTableName.authorid = $authorsTableName.id",
array("authorname"))
->order("$authorsTableName.authorname asc");
$result = $bugTable->fetchAll($select);
return $result;
}
But to do this you have to turn off ZF's table integrity checking (setIntegrityCheck(false) above), which means you won't be able to directly call save() on the resulting rows. But if it's for a read-only purpose, it will work.
If you needed to save rowsets back to the database, you may have to first select the author ID's from Model_Authors in the order you want them, and then re-order your Model_Bugs query accordingly. It's messier but it can work.