I was under the impression that we had to declare - public $name = 'ModelName'; in the model exclusively for PHP4. Now that cakephp no longer supports PHP4 I thought there no longer needs to be $name declaration in the model. The cookbook still has instructions to include it though: http://book.cakephp.org/2.0/en/models.html
The models work fine without it from what I see. What is it used for, and why do I need it?
Thanks!
It is just a leftover from the earlier cookbooks. See for example http://book.cakephp.org/2.0/en/models/model-attributes.html#name, it mentions PHP4 compatibility even though CakePHP 2.0 no longer works with PHP4.
So, to answer your question: no, you do not have to declare $name in your models.
you dont need need $name in any of your classes.
not even in 1.3 (who still uses php4 anyway^^), but especially not in 2.0.
I wrote an enhanced UpgradeShell which removes those unnecessary chunks from all class files:
http://cakephp.lighthouseapp.com/projects/42648/tickets/2117-improvements-for-20-upgrade-shell
but this new command I didnt yet add to the ticket patch.
I called the command "name"
/**
* Remove name (lib, controller, model, view, component, behavior, helper, fixture)
*
* #return void
*/
public function name() {
$libs = App::path('Lib');
$views = App::path('views');
$controllers = App::path('controllers');
$components = App::path('components');
$models = App::path('models');
$helpers = App::path('helpers');
$behaviors = App::path('behaviors');
$this->_paths = array_merge($libs, $views, $controllers, $components, $models, $helpers, $behaviors);
$this->_paths[] = TESTS . 'Fixture' . DS;
if (!empty($this->params['plugin'])) {
$pluginPath = App::pluginPath($this->params['plugin']);
$this->_paths = array(
$pluginPath . 'Lib' . DS,
$pluginPath . 'Controller' . DS,
$pluginPath . 'Controller' . DS . 'Component' .DS,
$pluginPath . 'View' . DS,
$pluginPath . 'View' . DS . 'Helper' . DS,
$pluginPath . 'Model' . DS,
$pluginPath . 'Model' . DS . 'Behavior' . DS,
$pluginPath . 'Test' . DS . 'Fixture' . DS,
$pluginPath . 'libs' . DS,
$pluginPath . 'controllers' . DS,
$pluginPath . 'controllers' . DS . 'components' .DS,
$pluginPath . 'views' . DS,
$pluginPath . 'views' . DS . 'helpers' .DS,
$pluginPath . 'models' . DS,
$pluginPath . 'models' . DS . 'behaviors' . DS,
$pluginPath . 'tests' . DS . 'fixtures' . DS,
);
}
$patterns = array(
array(
'remove var $name = ...;',
'/\bvar\s*\$name\s*=\s*(.*);/',
''
),
array(
'remove public $name = ...;',
'/\bpublic\s*\$name\s*=\s*(.*);/',
''
),
);
$this->_filesRegexpUpdate($patterns);
}
Related
I'm looking to grab admin user emails to use within a contact form. I have 2 admins that I've assigned. I'd like to grab them from the database. Where is/what is the designation point that I can use when I make the sql statment?
You can do the following
global $wpdb;
//comma separated list
$admins=$wpdb->get_var('select group_concat(`user_email`) as `admin_emails` from `' . $wpdb->prefix . 'users` as `users` inner join `' . $wpdb->prefix . 'usermeta` as `usermeta` on `usermeta`.`user_id`=`users`.`ID` where `meta_key`=\'wp_user_level\' and `meta_value` in (8,9,10);');
//array of associated arrays
$admins=$wpdb->get_results('select `user_email` from `' . $wpdb->prefix . 'users` as `users` inner join `' . $wpdb->prefix . 'usermeta` as `usermeta` on `usermeta`.`user_id`=`users`.`ID` where `meta_key`=\'wp_user_level\' and `meta_value` in (8,9,10);', ARRAY_A);
<?php $user_info = get_userdata(1);
echo 'Username: ' . $user_info->user_login . "\n";
echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
echo 'User ID: ' . $user_info->ID . "\n";
?>
You can pass the User id herer.....
The first thing to do is to create the function. To do so, paste the following code in your functions.php file:
function getUsersByRole($role) {
$wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
return $wp_user_search->get_results();
}
Once done, you can call the function this way:
$editors = getUsersByRole('Administrator');
foreach($editors as $editor){
//$editor now holds the user ID of an editor
}
I'm combining about 20 excel sheets (user uploaded) using PHPExcel into a report. This is done by using this bit of code:
$filename = APP . 'docs' . DS . 'reports' . DS . $Workbooks[0]['Report']['company'] . '.' . $Workbooks[0]['Report']['report_name'] . '.' . $Workbooks[0]['Report']['extension'];
$blacklist = array('Company Organogram');
$MasterFile = $this->PhpExcel->createMasterFile($filename,$company . '.' . $title,$colour);
foreach($Workbooks as $Workbook) :
if(!in_array($Workbook['Report']['report_name'],$blacklist)) {
$file = APP . 'docs' . DS . 'reports' . DS . $Workbook['Report']['company'] . '.' . $Workbook['Report']['report_name'] . '.' . $Workbook['Report']['extension'];
$MasterFile = $this->PhpExcel->addExternalWorksheet($MasterFile,$file,$company . '.' . $title,$colour);
#unlink($file);
}
endforeach;
$this->PhpExcel->saveToDisk(APP . 'docs' . DS . 'reports' . DS . 'Weekly Reports for Week ' . (date('W') + 1) . '.xlsx',$MasterFile);
I'm working with CakePHP, so there is a helper. saveToDisk simply saves the file to the disc after which it is attached and sent to a mailing list.
All the above works fine. But I'm having issues where the combining process draws in extra lines. For instance, this:
becomes this:
How would I fix this? Is there something I should set so it doesn't draw in all the lines?
How do I convert this to a prepared statement using mysqli & php?
$db->executeQuery('Update product Set prod_ops_txt = replace(prod_ops_txt, "/' . $img . '.jpg","") Where prod_ops_txt Like ("%/' . $img . '.jpg%")');
i'm looking to do something like this:
$stmt = $mysqli->prepare('Update product Set prod_ops_txt = replace(prod_ops_txt, ?,"") Where prod_ops_txt Like ("%?%")');
$thisImg = '/' . $img . '.jpg';
$stmt->bind_param('ss', $thisImg, $thisImg);
$stmt->execute();
$stmt->close();
It took a bit of fiddling but I finally found the proper syntax myself:
$stmt = $mysqli->prepare('Update product set product_options_text = replace(prod_opts_txt, ? ,"") Where prod_opts_txt Like (?)');
$thisImg = '/' . $img . '.jpg';
$thisImgMatch = '%/'. $img . '.jpg%';
$stmt->bind_param('ss', $thisImg, $thisImgMatch);
$stmt->execute();
$stmt->close();
# Cron Job for Cakephp #
I have create 1 file cron_scheduler.php which is copy of wamp/www/projectname/app/webroot/index.php file and it is also in the same folder
i have tried to run throw command prompt but didn't get any result.
how to run cronjob for Cakephp ? i don't have any idea
if anyone knows , please help me.
Thanks in advance.
<?php
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
/**
* These defines should only be edited if you have cake installed in
* a directory layout other than the way it is distributed.
* Each define has a commented line of code that explains what you
would change.
*
*/
if (!defined('ROOT')) {
//define('ROOT', 'FULL PATH TO DIRECTORY WHERE APP DIRECTORY IS
//LOCATED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
//You should also use the DS define to seperate your directories
define('ROOT', dirname(dirname(__FILE__)));
}
if (!defined('APP_DIR')) {
//define('APP_DIR', 'DIRECTORY NAME OF APPLICATION';
define('APP_DIR', basename(dirname(__FILE__)));
}
/**
* This only needs to be changed if the cake installed libs are located
* outside of the distributed directory structure.
*/
if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
//define ('CAKE_CORE_INCLUDE_PATH', FULL PATH TO DIRECTORY WHERE
//CAKE CORE IS INSTALLED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
//You should also use the DS define to seperate your directories
define('CAKE_CORE_INCLUDE_PATH', ROOT);
}
///////////////////////////////
//DO NOT EDIT BELOW THIS LINE//
///////////////////////////////
if (!defined('WEBROOT_DIR'))
{
define('WEBROOT_DIR', 'webroot');
}
if (!defined('WWW_ROOT')) {
//define('WWW_ROOT', dirname(__FILE__) . DS);
define('WWW_ROOT', dirname(__FILE__) . DS . 'webroot' . DS);
}
if (!defined('CORE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', CAKE_CORE_INCLUDE_PATH .
PATH_SEPARATOR .
ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'));
define('APP_PATH', null);
define('CORE_PATH', null);
} else {
define('APP_PATH', ROOT . DS . APP_DIR . DS);
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
}
}
require CORE_PATH . 'cake' . DS . 'bootstrap.php';
define('CRON_DISPATCHER',true);
if($argc == 2)
{
$Dispatcher= new Dispatcher();
$Dispatcher->dispatch($argv[1]);
}
?>
> i have tried to run throw command promt but didn't get any result
> how to run cronjob for Cakephp ? i don't have any idea
if anyone knows , please help me.
Thanks
Step1: Create a shell file with name ReminderShell.php and Path should be PROJECT_DIR_PATH/PROJECT_NAME/app/Console/Command/ReminderShell.php Copy below script and paste it
class ReminderShell extends Shell {
var $tasks = array('Mail');
function main() {
$this->Mail->enroll_reminder();
}
}
Step2: Create Task file with name MailTask.php and path should be PROJECT_DIR_PATH/PROJECT_NAME/app/Console/Command/Task/MailTask.php
<?php
App::uses('CakeEmail', 'Network/Email');
class MailTask extends Shell {
var $uses = array('Contact');
public function enroll_reminder() {
$Email = new CakeEmail();
$Email->config('default');
$reminder = $this->Contact->find('all');
if (!empty($reminder)) {
foreach ($reminder as $val) {
$id = $val['Contact']['id'];
$name = $val['Contact']['first_name'];
$email = $val['Contact']['email'];
$Email->template('reminder')
->viewVars(array('fname' => $name))
->emailFormat('html')
->subject('xyz.com: Enrollment Reminder')
->to($email)
->from('noreply#xyz.com');
if ($Email->send()) {
$update_val['Contact']['id'] = $id;
$update_val['Contact']['enroll_reminder'] = 'sent';
$update_val['Contact']['enroll_reminder_date'] = date("Y-m-d H:i:s");
$this->Contact->save($update_val['Contact']);
$this->out($email.' Mail Sent');
}
}
}
}
Step3: Create a email template with name reminder.ctp and path should be PROJECT_DIR_PATH/PROJECT_NAME/app/View/Emails/html/reminder.ctp
Step4: Create email.php in config directory
Step5: Run below command in Terminal: Console/cake Reminder
PROJECT_DIR_PATH/PROJECT_NAME/app Console/cake Reminder
For complete files checkout from https://github.com/pankajkumarjha2010/cronjob-in-cakephp2.3.x
For cronjobs in CakePHP, you may have a look at http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html. This works fine for me.
But please note, sometimes there are not all the environmental parameters available. I ran into this problem when accessing env('HTTP_HOST') within the cronjob methods. But when considering this, it should be no problem.
I need to retrieve the mysql connection settings and store them as variables. I know that the file configuration.php contains what I need. I just don't know how to call it accurately. Here are the 4 variables (and associate values) that I need:
1) $host = public $host from config file
2) $user = public $user from config file
3) $password = public $password from config file
4) $db_name = public $db from config file
$info =& JFactory::getApplication();
$info->getCfg('host');
$info->getCfg('user');
$info->getCfg('db');
// defined in libraries/joomla/application/application.php (380)
function getCfg( $varname ) {
$config =& JFactory::getConfig();
return $config->getValue('config.' . $varname);
}
looking at the code in joomla application you can also do the following :
$config =& JFactory::getConfig();
and get most if the info you want .