Here is the Model that I've written:
public function getMockTestResultDetails($studentID){
$mockTestResults = array();
$mockTestResults = $this->find('all',array(
'joins' => array(
array(
'table' => 'exam',
'alias' => 'exm',
'type' => 'LEFT',
'conditions' => array('StudentExam.TEMPLATEEXAM=exm.ID' ),
)
),
'fields' => array('exm.ENTRANCEEXAM', 'exm.NAME','count(StudentExam.TEMPLATEEXAM)' ,'StudentExam.IMPROVEMENT_INDEX' ,`StudentExam.EXAM_COMPLETED_ON`,'StudentExam.PHASE_ONE',
'StudentExam.PHASE_TWO','StudentExam.PHASE_THREE','StudentExam.PHASE_FOUR','StudentExam.PHASE_INTERN'),
//'group' => array(`StudentExam.TEMPLATEEXAM`),
'conditions' => array('exm.EXAM_TYPE' => 'MOCK','StudentExam.STUDENT' => $studentID,'StudentExam.STATUS' => 'COMPLETED'),
));
return $mockTestResults;
}
When I comment the line where 'group' is it works and I get a output but it is partial/incomplete output. I must use group to obtain the right answer. The same query when executed on phpmyadmin it works fine but it is not working in cakephp. Am I missing something here or what please suggest me the solution.
Related
I am building an onsite payment method and among other things I need to save data to a custom db table, after recieving a callback from an external API service.
This is the function:
// callback from external service acting as client
function _api_callback() {
global $user;
$data = json_decode(file_get_contents("php://input"), $assoc = true);
$date = $date['year'] . '-' . $date['month'] . '-' . $date['day'];
$timestamp = strtotime($date);
db_insert('postback')
->fields(array(
'user_id' => $user->uid,
'data' => $data,
'created_date' => $timestamp,
))
->execute(); // save data from external service
}
In the site's access log I can see that my site responds with a 500 code when the external callback arrives.
However if I comment out everything in that function the external callback recieves a 200 response code.
So there is some thing going wrong, when this callback is recieved. It's hard to debug since the callback from the external API service starts a whole new session.
The custom table "postback" was successfully created in the .install file of my custom module and I have checked in phpMyAdmin that the table is present and well. This is the schema() function in the .install file:
function module_payments_schema() {
$schema['postback'] = array(
'description' => 'Data saved from API postback URL.',
'fields' => array(
'id' => array(
'description' => 'Auto incremental id.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'user_id' => array(
'description' => 'User id for the order.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE),
'order_id' => array(
'description' => 'Current order number.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'data' => array(
'description' => 'Postback data from external API.',
'type' => 'varchar',
'length' => 1020,
'not null' => TRUE,
'default' => ''),
'created_date' => array(
'description' => 'Created date and time (yyyy-mm-dd H:i:s).',
'type' => 'varchar',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
),
'primary key' => array('id'),
);
return $schema;
}
Anyone who can see what's wrong?
It turns out I needed to format the date value like this...
date('Y-m-d H:i:s');
... in order to work with the mysql DATETIME type.
So the working function now looks like this:
function _api_callback() {
global $user;
$data = json_decode(file_get_contents("php://input"), $assoc = true);
db_insert('postback')
->fields(array(
'user_id' => $user->uid,
'data' => $data,
'created_date' => date('Y-m-d H:i:s');
))
->execute(); // save data from external service
}
So I have created a category in wordpress where I post some events. I have created two custom field where I insert two dates ( duration of the event ). Now my client wants to have 3 sort options ( current, upcoming, past ) that should be present in a dropdown menu.
For that i have managed to create this:
if($_ISSET('sort') && $_GET['sort'] == 'upcoming') {
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'm77_datefrom',
'value' => date('Y-m-d'),
'compare' => '>'
),
),
);
}
else if($_ISSET('sort') && $_GET['sort'] == 'past'){
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'm77_datefrom',
'value' => date('Y-m-d'),
'compare' => '<'
),
),
);
}
The problem is that I cannot insert the "sort" parameter.
Url structure : domain.com/category/events
Any ideas how can i solve this problem ? Or is there a better way to do this ?
I am trying to fetch the data from three table in cakephp 3.0
There is three table
1) size_category
2) sizes
3) sizerelations
Below is the file wise code
Model SizeCategoryTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->table('size_category');
$this->displayField('sizeCat_Id');
$this->primaryKey('sizeCat_Id');
// used to associate the table with user table (join)
$this->belongsTo('Users', [
'className' => 'Users',
'foreignKey' => 'CreatedBy',
'propertyName' => 'user'
]);
$this->hasMany(
'Sizerelations', [
'className' => 'Sizerelations',
'foreignKey' => 'Scat_Id',
'propertyName' => 'sizerelations'
]
);
}
Controller SizeCategoryController
public function index($id = null)
{
$customQuery = $this->SizeCategory->find('all', array(
'contain' => array(
'Users',
'Sizerelations' => array(
'Sizes' => array(
'fields' => array('id', 'Size_Code')
)
)
)
));
//debug();die();
$this->set('sizeCategory', $this->paginate($customQuery));
$this->set('_serialize', ['sizeCategory']);
}
I am greeting the error of Sizerelations is not associated with Sizes
I am trying to get relation where companies table have primary key companyID and division table have Foreign key companyID , what I need in where clause is WHERE companies.companyID = division.companies
relation in my model is :
public function relations()
{
return array(
'company' => array(self::BELONGS_TO, 'Companies', 'CompanyID'),
);
}
My Model->search() function is
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.CompanyID', $this->CompanyID, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
and my admin.php view is:
<?php
$this->breadcrumbs = array(
'Divisions' => array('index'),
'Manage',
);
$this->menu = array(
array('label' => 'List Divisions', 'url' => array('index')),
array('label' => 'Create Divisions', 'url' => array('create')),
);
");
?>
<div class="row">
<?php
$this->renderPartial('_dropdownfilter', array(
'model' => $model,
));
?>
</div><!-- end dropdown partial form -->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
?>
You need to add together=true to your criteria.
$criteria->together = true;
It'll add join to query. Some information about lazy loading http://www.yiiframework.com/wiki/527/relational-query-lazy-loading-and-eager-loading-with-and-together/
If you want to display company name,just do this in view.Don't change anything in model->search().
array(
'name'=>'Name',
'value'=>$model->company->name //here name is column name in company table.
),
In your gridview code do the following changes.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'companies',//fied from division table which refers to companyId from company table.
'header' => 'Company',
'value' => '$data->company->company_name'
),
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
And in your model->search()
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.company_name', $this->companies, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
I am having an issue with CakePHP 1.3 and a HABTM relationship. I have 2 models, Offer and Device. An Offer can be on multiple Devices, and a Device has multiple Offers. I have an devices_offer.php model and join table. What is happening is when an Offer is Edited, it creates a 2nd, unfinished entry, which contains the edit in both the offers table and the join table. The original Offer stays the same. Another thing is, an Offer also has a relationship with a Business (model). A Business owns an offer. The reason I say this, is the duplicate Offer entry that is created when editing an Offer does NOT contain the business name in the entry. So I'm wondering if this relationship is messing something up...Also, When a Device is edited, It duplicates ALL the offers that are related to the device, AND keeps the old ones. The code is really nothing more than baked controllers, and I followed the CakePHP docs to create the HABTM:
offers_controller.php (edit function):
if ($this->Offer->save($this->data)) {
$this->Session->setFlash(__('The offer has been saved',true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The offer could not be saved. Please, try again.',true));
}
devicesOffer contains:
var $belongsTo = array('Device' => array(
'className' => 'Device',
'foreignKey' => 'device_id',
'conditions' => '',
'fields' => '',
'order' => ''),
'Offer' => array(
'className' => 'Offer',
'foreignKey' => 'offer_id',
'conditions' => '',
'fields' => '',
'order' => ''
));
Offer Model:
var $hasAndBelongsToMany = array(
'Device' => array(
'className' => 'Device',
'joinTable' => 'devices_offers',
'foreignKey' => 'offer_id',
'associationForeignKey' => 'device_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => '',
'with' => 'DevicesOffer'
)
);
Device Model:
var $hasAndBelongsToMany = array(
'Offer' => array(
'className' => 'Offer',
'joinTable' => 'devices_offers',
'foreignKey' => 'device_id',
'associationForeignKey' => 'offer_id',
'unique' => true,
'conditions' => 'Offer.active = 1 AND Offer.expires > now()',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
UPDATE
I have Offers now editing / saving correctly. However, when I update / edit a Device. All the offers related to that device are duplicated ONLY in devices_offer. I am assuming this is because the ID's are not there. However, how would I set the ID's of the offers that are related to the device when saving??