i'm trying to develop a simple search form using cakedc plugin, i followed step by step the instructions , but i got the next error:
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'validateSearch' at line 1
SQL Query: validateSearch
i don't know what i got wrong, can you help me?, this is what i've got, Thank you.
In the Controller:
class ProductosController extends AppController {
public $name = 'Productos';
public $uses = array('Empleado','Cliente', 'Mesa','Producto', 'Productotipo','Productos');
public $components = array('Search.Prg');
public $presetVars = true;
public $paginate=array();
public function ventas(){
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->Producto->parseCriteria($this->Prg->parsedParams());
$this->set('productos', $this->paginate());
}
In the Model:
class Producto extends AppModel {
public $name = 'Producto';
public $displayField = 'productotipos_id';
public $belongsTo = array('Productotipo'=>array('className'=>'Productotipo','foreignKey'=>'productotipos_id','conditions'=>'','order'=>''));
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
'nombre' => array('type' => 'like')
);
In the view
<?php echo $this->Form->create('Producto', array('url' => array_merge(array('action'=>'ventas'), $this->params['pass'])));?>
<fieldset>
<legend>Pedido</legend>
<?php
echo $this->Form->input('Producto.nombre', array('div'=>false, 'empty'=>true));
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
</fieldset>
Try to reorder $uses that Controller's model will be the first:
public $uses = array('Producto', 'Empleado', 'Cliente', 'Mesa', 'Productotipo');
Should help. Don't know why, but probably some of methods from CakeDC Search plugin depends on first item in this array.
SQL Query: validateSearch
Is the usual error when you try to call a model method that does not exist.
So whatever model you try to paginate (it was not a good idea to not paste the class declarations...) does not use the searchable behavior for some reason.
In the case the model is the correct one the behavior is not loaded for some reason that you'll have to figure out.
Related
I'm new to cakephp3. I would like to know if it is possible to add a new entry form controller1 to table2.
It is a login form. So I would like to authenticate first whether the user is registered, then after authenticating, I would like to save the details to another table.
class UsersController extends AppController
{
public function login()
{
$user = $this->Users->newEntity();
if($this->request->is(['post']))
{
$user = $this->Auth->identify();
if($user)
{
$logs = TableRegistry::get('AttendsTable');
$log->username = 'lorem ipsum';
$log->datenow = '2018-11-10';
$log->tin = '12:42:00';
$log->tout ='12:42:00';
$logs->save($log);
$this->Auth->setUser($user);
$this->redirect(['action'=>'index']);
}
}
}
I am getting an error:
"Argument 1 passed to Cake\ORM\Table::save() must implement interface Cake\Datasource\EntityInterface, instance of stdClass given, called in C:\xampp\htdocs\TimeStamps\src\Controller\UsersController.php on line 32"
Your code isn't working because the Model->save() function expects an entity. In your snippet $log is not defined first, so PHP just makes it a stdClass (gotta love high-level programming :p).
Something like this should work:
$logs = TableRegistry::get('AttendsTable');
$log = $logs->newEntity([
'username' => 'lorem ipsum',
'datenow' => '2018-11-10',
// ...
]);
$logs->save($log);
Moreover, I've heard say it's better to use $this->loadModel() instead of TableRegistry::get():
$this->loadModel('ExternalModel');
// Now available:
$this->ExternalModel->//...
Cookbook > Controllers > Loading Additional Models
I have simple model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserExt extends Model
{
protected $table = 'users_ext';
public $timestamps = false;
}
In controller i try to display some data from users_ext in controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
use App\UserExt;
class RegisterController extends Controller
{
public function registerCheck(Request $request){
$user_ext=UserExt::latest()->get();
return response()->json(array(
'users'=>$user_ext,
));
}
}
But i got an error with:
Column not found: 1054 Unknown column 'created_at' in 'order clause' (SQL: select * from users_ext order by created_at desc)
I thought, $timestamps=false will disable managing timestamp by Eloquent.
Do i have to do something else?
Would you mind showing your scopeLatest method in your model? I think the problem lie there, probably orderBy set to created at.
I have worked with examples from a few different StackOverflow posts to get data from related tables in CI, but haven't been successful in connecting my tables in the "where" part, and I'd like to understand how to translate that from a MySQL query to the CI MVC.
This is the MySQL query that works to get the dctag fields from the dctags table where the dctag_id in the articles table matches:
$dctagger = $article['dctag_id'];
$query = "SELECT `dctitle`, `dctag_id` FROM dctags WHERE dctag_id = '$dctagger'";
$query_run = mysql_query($query);
This is the code for the Model, Controller, and View files:
Model -
public function get_article_dctag() {
$this->db->select('*')
->from('dctags')
->where('dctag_id', $this->$data['dctag_id']);
$query = $this->db->get();
return $query->result_array(); }
Controller -
public function article($slug) {
$data['article'] = $this->article_model->get_article($slug);
$data['dctag'] = $this->article_model->get_article_dctag();
$this->load->view('templates/header-article', $data);
$this->load->view('article', $data);
$this->load->view('templates/footer-home'); }
View -
<?php foreach ($dctag as $dctag_item): ?>
<title><?php $dctag_item['dctitle']; ?></title>
<?php endforeach; ?>
Error -
"A PHP Error was encountered... Severity: Notice... Message: Undefined variable: data...Filename: models/article_model.php" (see Model code above)
The problem is the variable $this->... and I don't know what that needs to be. Thanks for explanations/clarification -- in advance!
If your dctag_id is in $data['article'] then use following code
On Controller
public function article($slug)
{
$data['article'] = $this->article_model->get_article($slug);
$data['dctag'] = $this->article_model->get_article_dctag($data['article']['dctag_id']);
$this->load->view('templates/header-article', $data);
$this->load->view('article', $data);
$this->load->view('templates/footer-home');
}
On your Model
public function get_article_dctag($id)
{
$this->db->select('*')
->from('dctags')
->where('dctag_id', $id);
$query = $this->db->get();
return $query->result_array();
}
In the controller you should pass the variable on the get_article_dctag($var)
This will be the reference on your model get_article_dctag() and this should have a parameter of what you are searching for. In your structure it seems that Model cant locate your $data['dctag_id'] in your function
In the controller
$dctag_id = 2
$this->article_model->get_article_dctag($dctag_id);
In your model
function get_article_dctag($dctag_id){
$this->db->where('dctag_id',$dctag_id);}
Hopes it will help you a lot
I have two controllers (Feeds & Items), on the Items add view, I want some information from the Feeds table visible to the user.
So in the Items Controller I'm trying to access the Feed model, everything I have come across recommends to use loadModel but when I try to load the Items add view, I getting the following error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
SQL Query: SHOW TABLES FROM
Please note there is no relationship setup between these two tables.
Feed Model
App::uses('AppModel', 'Model');
class Feed extends AppModel {}
Item Model
App::uses('AppModel', 'Model');
class Item extends AppModel {}
Item Controller
App::uses('Feed','Model');
class ItemsController extends DirectResponseAppController {
function add() {
if ($this->request->is('post')) {
$this->Item->create();
if ($this->Item->save($this->request->data)) {
$this->Session->setFlash(__('Your item has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your item.'));
} else {
$this->loadModel('Feed');
$xxx = $this->Feed->find('all');
}
}
}
Any help appreciated.
** EDIT **
After a bit more digging, the problem seems to be with this line:
$xxx = $this->Feed->find('all');
I'm just not sure why.
If your Feed is inside a plugin (DirectResponse), you must use loadModel like this:
$this->loadModel('DirectResponse.Feed');
I have a fresh out o' the git repo CakePHP application. I just made a new model (Ingests) to track what data our system brings in. I wanted to make functions start() and end(), but end is protected so I switched to begin() and finish().
No matter what I do, CakePHP is trying to execute the model function names verbatim as the SQL queries. I have another model in this app that I've been working on this week that has not had this issue at all. Creating a new table/model today is when the problem appeared.
IngestsController.php
public function test(){
$this->autoRender = false;
//$result = $this->Ingest->finish();
$result = $this->Ingest->xyz();
debug($result);
}
Ingests.php Model
public function finish($id){
return 'giraffe';
}
public function xyz(){
return 'abc';
}
Output:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'xyz' at line 1
SQL Query: xyz
I tried xyz() because there is no way xyz() could be a protected/not allowed function name... but apparently it's just as bad a choice as finish(). If I run the finish() function, I get the same output... "SQL Query: finish"
Check your file name, the name for your file name model should be Ingest.php instead of Ingests.php also check your class declaration:
<?php
App::uses('AppModel', 'Model');
class Ingest extends AppModel { //Make sure the model name is singular
public function finish($id){
return 'giraffe';
}
public function xyz(){
return 'abc';
}
}