How to Get Field Name With Query in Zend Framework
Test "Select * From Test1,Test2"
how to get all field name in this query
Zend Frame work cam do it?
Untested, but I believe the query is returned as an associative array (where the column name is the key), and so you can loop through the first record and pick up the column names, e.g.
$sql = 'Select * From Test1,Test2';
$result = $db->fetchAll($sql, 2);
foreach ($result[0] as $key => $value) {
echo $key;
...
}
You could also issue $db->describeTable('Test1'), etc. before or after the query which would provide you with all the meta information you need. This query is pretty expensive though, so make sure to cache the response.
Also, if you're using a model which extends Zend_Db_Table_Abstract, then you should have all this information already. In this case, all you need to do is access the protected property $_metadata.
HTH
Related
Is there a way to write a CakePHP query to return all fields (columns) except one via find()? Or do I need to use the fields parameter and actually list all the fields, except the excluded field?
For example, if I have a database table (model), Company, with these fields:
id
name
street
city
state
zip
phone
Normally, $this->Company->find('all') would return all the fields. I want to exclude the phone field from the resultset.
$fields = array_keys($this->Company->getColumnTypes());
$key = array_search('phone', $fields);
unset($fields[$key]);
$this->Company->find('all', array('fields' => $fields));
For more info, please have a look at http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html#model-getcolumntypes
I can think of a couple ways to do this
Write out all the fields you want to include in the fields parameter. As mentioned in the comment, you can use $this->Company->schema to get all the fields programmatically rather than writing them out.
Unset the field you don't want after you get the data. You can do this in the model's afterFind function too.
New to CakePHP, so be gentle...
I'm trying to loop through a table to display all the read-only fields (labels and values).
In my edit function, I get the table
$this->set('invoice', $this->Invoice->read(null, $id));
In my view, I want to loop through the entire table schema and output the field names and values as labels like fieldName: value
Invoice Number: SVC00158
Invoice Date: 03/03/12
There are 37 fields in this table. I would rather not have to manually code for every one. I know to retrieve the input fields like this
echo $this->Form->input('purchaseOrderNumber');
but I can't seem to find a 'read-only' attribute for the input() method. Hope that makes sense.
Thanks for your time.
You could loop through the schema and output it like that:
// assumes $result contains model data
$schema = $this->Model->schema();
foreach ($schema as $field => $attrs) {
echo $result['Model'][$field];
}
just write:
<?=$this->Form->inputs();?>
If you are a beginner I suggest you look into Cake Bake (it will "bake" all your files from the console).
I mean the views, controllers, and models based on you current db schema so that will it for you and even apply some styling to it.
I hope that helps
I currently have a users table and a posts table. My posts table has a field for user_id, however, I am not sure how I would grab the users name field which is in the users table. I was thinking of using the models afterFind() method and then using SQL to select the data, but there has to be a better way than this. Also, on my view action, I am using the read() function to grab a single post. Would the models afterFind() kick in after it runs read()? If not, is there an equivalent such as afterRead()?
Just make an association of Post belongsTo User, and every regular find/read operation that has a sufficiently high recursive value will automatically fetch the user with each post.
$post = $this->Post->find(...);
echo $post['User']['name'];
you have to go like below :
$this->Cake->findById(7); Cake.id = 7
here , you can you use your user_id instead of 7 like..
Find BY CAKE PHP
$this->Post->bindModel(array('belongsTo'=>'User'));
$post = $this->Post->findById($post_id);
$userName = $post['User']['name'];
Here are errors, I've typed it in eclipse (:
I've created a content type in Drupal 7 with 5 or 6 fields. Now I want to use a function to query them in a hook_view call back. I thought I would query the node table but all I get back are the nid and title. How do I get back the values for my created fields using the database abstraction API?
Drupal stores the fields in other tables and can automatically join them in. The storage varies depending on how the field is configured so the easiest way to access them is by using an EntityFieldQuery. It'll handle the complexity of joining all your fields in. There's some good examples of how to use it here: http://drupal.org/node/1343708
But if you're working in hook_view, you should already be able access the values, they're loaded into the $node object that's passed in as a parameter. Try running:
debug($node);
In your hook and you should see all the properties.
If you already known the ID of the nodes (nid) you want to load, you should use the node_load_multiple() to load them. This will load the complete need with all fields value. To search the node id, EntityFieldQuery is the recommended way but it has some limitations. You can also use the database API to query the node table for the nid (and revision ID, vid) of your nodes, then load them using node_load_multiple().
Loading a complete load can have performance impacts since it will load way more data than what you need. If this prove to be an issue, you can either try do directly access to field storage tables (if your fields values are stored in your SQL database). The schema of these tables is buld dynamicaly depedning on the fields types, cardinality and other settings. You will have to dig into your database schema to figure it out. And it will probably change as soon as you change something on your fields.
Another solution, is to build stub node entities and to use field_attach_load() with a $options['field_id'] value to only load the value of a specific field. But this require a good knowledge and understanding of the Field API.
See How to use EntityFieldQuery article in Drupal Community Documentation.
Creating A Query
Here is a basic query looking for all articles with a photo that are
tagged as a particular faculty member and published this year. In the
last 5 lines of the code below, the $result variable is populated with
an associative array with the first key being the entity type and the
second key being the entity id (e.g., $result['node'][12322] = partial
node data). Note the $result won't have the 'node' key when it's
empty, thus the check using isset, this is explained here.
Example:
<?php
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->fieldCondition('field_news_types', 'value', 'spotlight', '=')
->fieldCondition('field_photo', 'fid', 'NULL', '!=')
->fieldCondition('field_faculty_tag', 'tid', $value)
->fieldCondition('field_news_publishdate', 'value', $year. '%', 'like')
->fieldOrderBy('field_photo', 'fid', 'DESC')
->range(0, 10)
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
if (isset($result['node'])) {
$news_items_nids = array_keys($result['node']);
$news_items = entity_load('node', $news_items_nids);
}
?>
Other resources
EntityFieldQuery on api.drupal.org
Building Energy.gov without Views
I have to run a query after every update and I want to know if there is a way to automate a $this->db->query() before every $this->db->update()
I'm using it for log.
you can write your own function in the file core/MY_Model.php to do that:
function queryThenUpdate($query,$update)
{
$query = $this->db->query($query);
//use as you need $query
$this->db->update($update['table'],$update['data']);
}
where:
$query is your actual query: SELECT * FROM ...
$update is an array of two elements $update['table'] is the table to update and $update['data'] is the updating data as specified on codeigniter active record's documentation
then make every model extend MY_Model
class Your_Model extend MY_Model
and every time you need to update something:
$this->Your_Model->queryThenUpdate($query,$update)
I believe you want to use codeigniters 'hook'