How to get Column Name With Zend DB - zend-framework

How to get Column Name With Zend DB

This is the correct answer, the older answers are wrong or outdated:
$cols = $table->info(Zend_Db_Table_Abstract::COLS);

$metadata = $db->describeTable($tableName);
$columnNames = array_keys($metadata);
http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.list-describe

Previous answer applies only to version < 2.
For current version of ZF (2.2) use:
$table = new Zend\Db\TableGateway\TableGateway('table', $Dbadapter, new Zend\Db\TableGateway\Feature\MetadataFeature());
$columns = $table->getColumns();
http://framework.zend.com/manual/2.2/en/modules/zend.db.table-gateway.html#tablegateway-features
http://framework.zend.com/manual/2.2/en/modules/zend.db.metadata.html

You could use the describeTable method

I like this way:
$table->info('cols');

Related

Backendless API documentation incorrect?

Here is the code:
let queryBuilder = DataQueryBuilder()
queryBuilder.setWhereClause(whereClause: "age = 47") <---but this doesn't exist in the code.
The "setWhereClause" is not a member of queryBuilder. Is there updated documentation somewhere?
is there something wrong in my pod? I just verified I have the latest version.
I got it to work with this statement:
queryBuilder.whereClause = "age = '47'"
I wish they would update their tuts.

Is there a way to add an ID to a file on includeJS array in Typo3 8?

Or in includeJSFooter I have tried
includeJSFooter {
initJavascript = path/to/file/init.js
initJavascript.id = initJavascript
}
but It does not work and I do not see any option in the docs but maybe I have missing something.
This is not possible. For this kind of inclusion you need to use page.headerData or page.footerData.
See: https://docs.typo3.org/typo3cms/TyposcriptReference/Setup/Page/Index.html#headerdata

How to get moodle module name from module id

I want to list the names of the modules (activities) in my course sections from moodle function or from db query.
I can get section details and module id from mdl_course_sections and mdl_course_modules tables I want to get name or tile put in every activity to be list down.
I already tried mod_frog_get_coursemodule_info($cm) but I hadn't any luck with it.
Use get_fast_mod_info() e.g.:
$modinfo = get_fast_modinfo($courseid);
foreach ($modinfo as $cm) {
echo $cm->modname;
}
I advise you :
$modinfo = get_fast_modinfo($courseid);
$cm = $modinfo->get_cm($moduleid);
Reference: https://docs.moodle.org/dev/Module_visibility_and_display#get_fast_modinfo_data
I found a solution to this with following code
$activity_modules = $DB->get_record('modules',array('id' =>$moduleid));
$dynamic_activity_modules_data = $DB->get_record($activity_modules->name,array('id' =>$instanceid));
echo $dynamic_activity_modules_data->name;
Use get_fast_modinfo() :
$cm = $modinfo->cms[$moduleid];
$modulename = $cm->modname;
This can be achieved in 2 lines within moodle structure:
global $DB;
$moduleName = $DB->get_field('modules','name',array('id' => $moduleID));

Access _users table in SocialEngine?

i got a (simple?) problem accessing _users table in SocialEngine. To access tables in SE4 i use this:
$table = Engine_Api::_()->getDbTable(tablename,tablegroup);
and this works fine for _user_online (->getDbTable('online','user')) etc. but I don't know how to access _users table (which has not tablegroup prefix).
i tried:
->getDbTable('users')
->getDbTable('','users')
->getDbTable(null,'users')
->getDbTable('foo','what_a')
no way.
Engine_Api::_()->getItemTable('user');
I suggest you read SocialEngine factory codes to find out answer to this kind of questions.
you are missing commas
try this
$table = Engine_Api::_()->getDbTable('users','user');
it will give you users table object. hope this will help
You can use below 2 methods to get the "engine4_users" table object:
1) $userTable = Engine_Api::_()->getItemTable('user);
2) $userTable = Engine_Api::_()->getDbTable('users', 'user');
Try this one.
$query=Engine_Db_Table::getDefaultAdapter()->select()
->from('engine4_yourtablename')
->where("your_field_name = ?", $variable)->limit(1);
$query= $query->query()->fetch();
Hope this will help.

Zend_Db - Building a Delete Query

I'm trying to recreate the following in Zend Framework and am not sure how to do it:
DELETE FROM mytablename WHERE date( `time_cre` ) < curdate( ) - INTERVAL 4 DAY
I was thinking something like:
$table = $this->getTable();
$db = Zend_Registry::get('dbAdapter');
$db->delete($table, array(
'date(`time_cre`) < curdate() - interval 4'
));
Does that seem correct?
What is the best way to handle something like this?
EDIT: Ack! Sorry, I was adapting this from a SELECT I was using to test and didn't change the syntax properly when I pasted it in. I've edited the example to fix it.
Figured it out...
public function pruneOld($days) {
$table = $this->getTable();
$db = Zend_Registry::get('dbAdapter');
$where = $db->quoteInto("DATE(`time_cre`) < CURDATE() - INTERVAL ? DAY", $days);
return $table->delete($where);
}
$table gets an reference of the table I want to edit...
$db grabs an instance of the database adapter so I can use quoteInto()...
$where builds the main part of the query accepting $days to make things a bit more flexible.
Create an action to call this method... something like:
public function pruneoldAction() {
// Disable the view/layout stuff as I don't need it for this action
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
// Get the data model with a convenience method
$data = $this->_getDataModel();
// Prune the old entries, passing in the number of days I want to be older than
$data->pruneOld(2);
}
And now hitting: http://myhost/thiscontroller/pruneold/ will delete the entries. Of course, anyone hitting that url will delete the entries but I've taken steps not included in my example to deal with this. Hope this helps someone.
I usually do this to delete a row
$table = new yourDB;
$row = $table->fetchRow($table->select()->where(some where params));
$row->delete();
try:
$db->delete($table, array(
'date(time_cre) < ?' => new Zend_Db_Expr('curdate() - interval 4')
));