the Model filed can't named 'model' in peewee? - peewee

I use peewee-async which is based on peewee to create a Model which has a field named 'model'
class Test(peewee.Model):
model = peewee.CharField(max_length=255)
class Meta:
database = DATABASE
db_table = 'test'
schema = 'opr'`
when I create a object,It happens a error:
File "/home/mwh/py3workplace/workplace/ad_tornado/handlers/opr.py", line 78, in post
yield from self.db.create(TaskReqEven,**insert_sql)
TypeError: create() got multiple values for argument 'model'
I tried to use other name replace 'model',It submitted correctly to database,so I
douted if the model name can't name 'model'?

Looks like the error is in some override you've created? Look at your code. Whatever "db.create()" is doing is the problem.

Related

CakePHP Access Multiple Database Table from a Model

I am using CakePHP 2.x
There are several database tables setup :
1) Fruit
2) Vege
3) Drink
I am able to access these database tables in a CONTROLLER using this line below. With this line, I am able to access these other tables.
public $uses = array('Get', 'Fruit', 'Vege', 'Drink');
My problem is when trying to access them in a MODEL. When I try this code below, an error occurs.
App::uses('AppModel', 'Model');
class Get extends AppModel {
public function getHistory( $limit ) {
$searchLimit = $limit;
$raw = $this->Fruit->find('all')
An error occurs at the line '$this->Fruit'.
Call to a member function find() on a non-object...
Any ideas how to call multiple database tables in a single MODEL ?
As accessing all the database tables work perfectly in the CONTROLLER, I did this in the CONTROLLER instead of the MODEL. It seems much easier and straight forward to do this in CONTROLLER.
A private function is created in the CONTROLLER, and accessed by other functions using '$this->myPrivateFunctionName()'
First you should read on model associations Model associations
Second, if that doesn't fit your needs (meaning there is no clear association between models, honestly I don't see connection between Get and Fruit) you can use ClassRegistry:
$Fruit = ClassRegistry::init('Fruit');
Before you can use ClassRegistry you must add this before class definition:
App::uses('ClassRegistry', 'Utility');
Associate model by $belongsTo, $hasMany or what ever relation you want.
than access by $this->Fruit->find()

Laravel Relationships (user-upload)

I get the following errors when i am trying to save the data with the file upload function because I have declared a new uploader_id to my table which should get its value from the relationship I wrote in the model.
Illuminate \ Database \ QueryException
SQLSTATE[HY000]: General error: 1364 Field 'uploader_id' doesn't have a default value (SQL: insert into `assets` (`filename`, `title`, `body`, `filepath`, `extension`, `realpath`, `mime`, `updated_at`, `created_at`) values (users.mov, New, dummy text data. , /uploads/User Test/2014-03-04_00:48, mov, /private/var/tmp/phpHDpmEI, video/quicktime, 2014-03-04 00:48:59, 2014-03-04 00:48:59))
In my models I defined the following
User Model: where assets its the table name for the uploads table in database
public function assets() {
return hasMany('assets');
}
Upload Model:
public function user() {
return belongsTo('user', 'uploader_id');
}
I have tried in my controller where i am using Upload::create(array()) function to post the data to the database, to add another parameter called 'uploader_id' => Auth::user()->id; but none of this work.
What is the best way to pass the uploader_id to the form each time the user uploads a file ?
That's what events are for:
class Asset extends Eloquent {
public static function boot()
{
static::creating(function($model)
{
$model->uploader_id = Auth::user()->id;
});
}
}
Also your relationships should reference their class -
return $this->hasMany('Asset');

Getting CakePHP to work with existing "non-Cake" database

I am building a new app based on multiple databases with many tables which don't follow any Cake conventions. I want to use CakePHP but I'm not sure if it's possible without the database in a Cake format.
Problems include:
Tables not named as Cake expects
Primary keys are not necessarily named id (e.g. it might be order_id)
Foreign keys are not necessarily named like other_table_id
Changing the database tables is not an option.
Is it possible to manually configure the schema in each model so that Cake will then know how the model relationships need to work? Or should I just give up on using Cake?
yes. you can still use CakePHP in your case.
Check out various Model attributes to fit your needs
http://book.cakephp.org/2.0/en/models/model-attributes.html.
e.g.
public $useTable = 'exmp' can be used to configure what table to use.
public $primaryKey = 'example_id'; can be used to configure the primary key's name
**Try this code sample............**
More detail here http://book.cakephp.org/2.0/en/models/model-attributes.html
<?php
class Example extends AppModel {
// Name of the model. If you do not specify it in your model file it will be set to the class name by constructor.
public $name = 'Example';
// table name example
public $useTable = example;
// example_id is the field name in the database
public $primaryKey = 'example_id';
}
?>

adding required property option not working

I'm trying to add property options to my model. I have a StringProperty and I added required=True but I'm still able to create an object with the required field being empty.
I tried it in the admin and also in my update form for the specific model so not sure what I'm doing wrong?
You can create it, but can you put it?
class x(ndb.Model):
author = ndb.StringProperty(required=True)
a = x()
a.put()
Fails with: BadValueError: Entity has uninitialized properties: **author**
Setting a value on the required property author allows you to save it:
a.author = "some_value"
The put can now succeed.
key = a.put()
and key is now:
Key('x', 5707702298738688)
or even key.urlsafe()
ahNzfnNoYXJlZC1wbGF5Z3JvdW5kcg4LEgF4GICAgICArpkKDKIBEDYwNTM4Njc2MzY2NTQwODA
Read more about storing data here.

Problem with altering model attributes in controller

Today I've got a problem when I tried using following code to alter the model attribute in the controller
function userlist($trigger = 1)
{
if($trigger == 1)
{
$this->User->useTable = 'betausers'; //'betausers' is completely the same structure as table 'users'
}
$users = $this->User->find('all');
debug($users);
}
And the model file is
class User extends AppModel
{
var $name = "User";
//var $useTable = 'betausers';
function beforeFind() //only for debug
{
debug($this->useTable);
}
}
The debug message in the model showed the userTable attribute had been changed to betausers.And It was supposed to show all records in table betausers.However,I still got the data in the users,which quite confused me.And I hope someone can show me some directions to solve this problem.
Regards
Model::useTable is only consulted during model instantiation (see the API documentation for Model::__construct). If you want to change the model's table on the fly, you should use Model::setSource:
if ( $trigger == 1 ) {
$this->User->setSource('betausers');
}
The table to use is "fixed" when the model is loaded/instantiated. At that time a DB connection object is created, the table schema is being checked and a lot of other things happen. You can change that variable later all you want, Cake is not looking at it anymore after that point.
One model should be associated with one table, and that association shouldn't change during runtime. You'll need to make another model BetaUser and dynamically change the model you're using. Or rethink your database schema, a simple flag to distinguish beta users from regular users within the users table may be better than a whole new table.

Resources