Zend Query Select - zend-framework

Hi i need to do a simple query but something is wrong. I have $name and $surname and i need to search the (possible multiple) id that rappresent that name and surname and put all the id, name and surname in a array
I do this query:
$result=$this->_db_table->select()->where('name=?',$name)
->where('surname=?', $surname)->query()
->fetchAll();
$array=$result->toArray();
return $array;
If i use
$result=$this->_db_table->fetchAll();
$array=$result->toArray();
return $array
it work correctly and i have an array whith all the value in the database in that table. What is wrong in my first code???

After doing this
$result=$this->_db_table->select()->where('name=?',$name)
->where('surname=?', $surname)->query()
->fetchAll();
$result is already an array its not an object . So simply use it instead of calling toArray on it.
Correct code wd be
$result=$this->_db_table->select()->where('name=?',$name)
->where('surname=?', $surname)->query()
->fetchAll();
return $result;

Related

CakePHP 3 - Exclude fields by default in query unless specifically selected

Title pretty much says it all. I have some tables with fields that contain a lot of data. To save some performance I would like to not SELECT these by default.
The emphasis on the new default behaviour, differentiating the question from e.g. Select all except one field in cakephp 3 query
Example:
$cities = $this->Cities->find();
// A $city does not include the field `shape` (which is a huge polygon)
$cities = $this->Cities->find(['id', 'name', 'shape']);
// A $city now does include the `shape` property
I looked at the accessible and hidden properties of an entity, but these don't seem to affect the SELECT statement.
EDIT: The selectAllExcept query seems usefull. I combined this with the beforeFilter event like this:
public function beforeFind($event, $query, $options, $primary)
{
$query->selectAllExcept($this, ['shape']);
}
This works well for empty queries, shape is now excluded. But now I have no control over the other fields that might want to include or not:
$this->Cities->find()->select(['id', 'shape']) will then also select the other fields because the selectAllExcept().
You can simple overwrite find('all') method in your table.
For example in UsersTable:
public function findAll(Query $query, array $options)
{
$query->selectAllExcept($this, ['password']);
return $query;
}
then in your controller:
// select all except password
$users = $this->Users->find();
debug($users);
OR
// we try to select some fields, without success
$users = $this->Users->find()->select(['id', 'username', 'password']);
debug($users);
OR
// we try to select some fields incl. password, with success
$users = $this->Users->find()->select(['id', 'username', 'password'], true); // <-- this overwrite select / selectAllExcept in custom finder
debug($users);

setting variable name with variable at the end - perl

$id = 1;
foreach (#products) {
$productid$id = param(product$id);
++$id;
}
I am trying to loop through the products array and create a new variable for each item in products for a form which uses checkboxes to select products which are for sale. I tried to use $productid$id and $productid"$id" but it returned syntax errors. Is there a way to achieve this?
I am trying to loop through the products array and create a new variable for each item in products.
While it is possible to create a new variable for each item, it's a bad idea. They have to be global, the syntax is ugly, and it's difficult to pass those variables around as a collection. Instead, use a hash or an array. In this case, since your items are indexed by number and there's no gaps an array makes the most sense.
my #product_params;
foreach my $product (#products) {
push #product_params, param($product);
}
For each $product in #products, it will add param($product) to #product_params. For example, the parameter for $products[5] is stored in $product_params[5].
Since this is a one-to-one mapping of #products to its parameters, its easier and faster to do the above with a map. A mapping runs each element of a list through a function to create a new list. $_ contains each element of #products in turn.
my #product_params = map { param($_) } #products;
Variables aren't interpolated in single quotes.
You're resetting the $id in each iteration of the loop.
Update: Your changes invalidated both the bullets above.
my $id = 1;
for my $product (#products) {
$product_id = param("product$id");
++$id;
}
Say you have four checkboxes named
product1
product2
product3
product4
Say product1 and product3 are checked.
The following will place 1 and 3 in #selected_product_ids:
my #selected_product_ids =
map { my ($id) = /^product(\d+)\z/; defined($id) && param($_) ? $id : () }
params();
If you have the list of all existing product ids in #product_ids, the following will do the same:
my #selected_product_ids =
grep { param("product$_") }
#product_ids;

Laravel 5 Eloquent with MongoDB - get array of column names from document

this is becoming frustrating beyond imagination.
I need to get the column names from a table using Eloquent ORM in Laravel 5 combined with MongoDB. I have found some examples, but none of them is working for me as they are probably made for SQL specifically. I tried this and this without success, any idea?
Thanks!
It would be best to use the raw() method in this case and use the native MongoCollection methods like find() to iterate over the collection and get the keys in the documents array:
// Returns an array of field names from a collection of User models.
$keys = DB::collection('users')->raw(function($collection)
{
$cursor = $collection->find();
$array = iterator_to_array($cursor);
$fields = array();
foreach ($array as $k=>$v) {
foreach ($v as $a=>$b) {
$fields[] = $a;
}
}
return array_values(array_unique($fields));
});
MongoDB doesn't have columns or tables - the whole point is that it's schemaless therefore there are no columns for you to get the names of.
Because every document can be different, you'd need to get all documents in your collection and build an array of unique keys that each document contains.
See this answer:
MongoDB Get names of all keys in collection

PHP SQL Return just returns "Array"

I've tried using something similar to the following PHP code to retrieve items from a Firebird database.
$sql = "select P_1,P_2,P_3 from p_players('$playerid', '')";
//This sends the SQL select statement to the db
$rs=$db->Execute($sql);
//This converts the SQL statement to an array
$result = $rs->GetArray();
echo $result;
However, the echo result that I receive keeps returning "Array" no matter what I select as the database to query. What am I doing wrong? Thanks for any assistance.
Try this one:
$sql = "select P_1,P_2,P_3 from p_players('$playerid', '')";
//This sends the SQL select statement to the db
$rs=$db->Execute($sql);
//This converts the SQL statement to an array
$result = $rs->GetArray();
print_r $result;

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'];