I have table name "classes" in database and when i create model with name "Class" ,it gives syntax error (obviously).Is there any way to remove this error without changing name of table in database ?
I would stay away from a model named "Class", it is a keyword of php and it may will (as pointed by #AD7six) cause trouble if instantiated like that.
You can do the following:
class MyClass extends AppModel {
public $useTable = 'classes';
}
Be sure that the controller ClassesController calls MyClass (with $uses), but besides that, you can use the model like any other model without worrying about reserved keywords.
I can't remember the name I used but I had a similar issue with cakephp and after some looking online it worked out to be so much easier to just change the table name. The naming conventions are quite strict and creating a work-around for this isn't worth it.
You could find a different name for your model and then link that model to your table using useTable
hope that helps
Related
Is there a way to override the default behavior of SS Data Objects such that when I assign a static $table_name property to my DataObject the dev/build does not create a table name with the DO name like it normally does?
For example I have this very small Data Object
<?php
class SalesRep extends DataObject {
private static $table_name = 'tbl_users';
}
I am trying to prevent creation of table salesrep on dev/build and also I would like the ORM to know that when I do a $Model->write(); I'm writing to the table tbl_users instead of table salesrep
This is currently not possible with SilverStripe 3.x. SilverStripe uses the "convention over configuration" principle and the database tables always have the same name as the related DataObject.
However, in SS4, with namespacing, you'll be able to define a tablename in your config. As #bummzack already noted, this is currently in alpha.
However, you might try and overwrite DataObject's getBaseTable(), which method like:
/**
* Get the name of the base table for this object
*/
public function baseTable() {
return 'tbl_users';
}
but i doubt it'll work without problems, cause in other places the baseTable property is - again - generated out of the class names.
This is part of using the ORM that is within SilverStripe and can take some getting used to. I would perhaps look at this in two different ways...
1) If your goal is to present a certain name to the user, but have a different table name then the solution is to use singular_name and plural_name and then you are free to name the DataObject however you wish...
class tbl_users extends DataObject {
private static $singular_name = 'Sales Rep';
private static $plural_name = 'Sales Reps';
...
}
..remember the whole point of the ORM is that the PHP class defines the table and it would make sense to keep the table name the same as you'd like to use in the code.
2) If it absolutely has to be a specific table then you can specify it as an external table/content and one of the following solutions might suit you best... "Save to external Table", "External Content Module" or "External Data Module"
I have a model generated as Parent from Yii.
class Parent extends AppModel {}
When I used it as follows, I got an error.
$parent = new Parent();
Fatal error: Cannot use 'Parent' as class name as it is reserved in E:\Customer\Qelasy\Project\QelasySecurity\web\SourceCode\protected\modules\user\models\Parent.php on line 14
As I understand, this is because Parent is a keyword in PHP, and Yii has generated this. Is there any workaround to make it work with Parent without changing the model name into Parents?
No. You cannot name a class Parent, period. It's a reserved keyword in the core language. Unless you change the core language, you cannot circumvent this restriction. Since class names aren't case sensitive, what would the following statement do?
public function foo() {
parent::bar();
}
Therefore: no naming of classes that clash with keywords.
No, you will have to rename your model to something else. Try and be more specific with your class name. What is this a parent of?
MyParent? UserParent? AppParent? Maybe use a synonym?
Parent is reserverd php keyword, cannot able use it as class name, use different name ,
class myclass extends AppModel {}
$myclass = new myclass();
Here is the link, you can find the list of keywords :http://php.net/manual/en/reserved.php
I'd like to use a different table prefix for my session database. So, in my config file I have my table prefix set as "pre1_", but I'd like my sessions to use a table with the prefix "pre2_" -- is this possible?
Thanks.
I remarked the line in my model so it would not be executed;
// public $tablePrefix = 'cc_';
Then, in my AppController.php beforeFilter I added
$this->modelname->tablePrefix=$this->Auth->user('company_prefix').'_';
In my users table of my authorization I have a field called company_prefix which is prepended to certain tables that are unique to that user.
I would love to see more on this subject.
I get a lot of 'Indirect modification of overloaded property DifferentController::$Modelname has no effect'
So I am looking for a way of doing this only when the model is added or the default of a controller. I don't want a beforeFilter() for every controller, so I think I will have to do some sort of isset()
For a 'company' prefix in Cakephp 2.1 - public $tablePrefix in the model and setting the tablePrefix in the controller, here is what I am doing and it is too many lines of code (one line of code per table that has a prefix per user) but it is all I can do right now.
Modela.php:
public $tablePrefix = 'anything_';
Modelb.php:
public $tablePrefix = 'anything_';
Modelc.php:
public $tablePrefix = 'anything_';
then in the public function beforeFilter() of the AppController
if (isset($this->Modela->tablePrefix)) {$this->Modela->tablePrefix = $this->Auth->user('company_code').'_'; }
if (isset($this->Modelb->tablePrefix)) {$this->Modelb->tablePrefix = $this->Auth->user('company_code').'_'; }
if (isset($this->Modelc->tablePrefix)) {$this->Modelc->tablePrefix = $this->Auth->user('company_code').'_'; }
I don't get the 'overloaded property' error this way, but in my schema, all my users have a valid company code. I also have for example the 'users' table is shared for all users (although only admins can get to a user record other than the logged in user)
However, I find this rather cumbersome, and am interested in a means that would not require every single model that is to be segregated by the model's $tablePrefix company_code setting to be defined as a line of code in the AppController. The point at which I really want to set the tablePrefix for a model is the instant I reference
$this->loadModel("Modelb");
But I spent 45 minutes playing with modelb.php in the Model directory and was not able to do much with the $tablePrefix property. When a model is instantiated or referenced, I should know, for those tables that need a $tablePrefix property, the required prefix, but I was not able to set the property dynamically at that point. (a dynamic static property? sounds like the wrong approach)
So, even though this works, I don't like it. It feels like I am using Fortran methods in a Lisp program.
within ModelasController.php:
$this->loadModel("Modelb");
$this->Modelb->tablePrefix = $this->Auth->user('company_code');
is the proper way to use a second company coded model, and I don't need the beforeFind logic.
So only the AppController has a line for every possible model in the beforeFilter to set the tablePrefix, just like above, using the php function isset to detect the model.
But when I pull a secondary model into a controller with the loadModel, I immediately follow that now by setting the tablePrefix to the company code, which I can get to in the controller. This reaches into the model object and overrides the tablePrefix static property.
I ended up changing the prefix in the database.php config file to the prefix I wanted my sessions table to have, then in each model set the $table_prefix property to the prefix those required. Seems a little weird, but it got the job done.
The underscore is a special character in Kohana and gets translated into a directory separator but since I have a bunch of existing code that uses underscores in table names, I want to know if it's possible to configure Kohana to understand that in some way.
One way to do this would be by putting your model classes in subdirectories of the Model folder.
For example, if you had a table called user_profiles, your directories would look like this:
application/
...classes/
......model/
.........user/
............profile.php
and profile.php would be like this:
<?php defined('SYSPATH') or die('No direct access.');
class Model_User_Profile extends ORM
{
}
I would recommend going with the above approach (we use it at the company where I work), as it's the "standard" Kohana way of doing things, so you will have fewer hassles down the road. However, if you need to for some reason, you could also use the _table_name property of the ORM class (see docs here):
application/
...classes/
......model/
.........userprofile.php
and profile.php would be like this:
<?php defined('SYSPATH') or die('No direct access.');
class Model_UserProfile extends ORM
{
protected $_table_name = 'user_profiles'; // <== manually setting table name
}
This can also be helpful if your tables don't quite follow the singular vs. plural convention Kohana uses.
Kohana doesnt replace underscores in table names. You can use everything you want in plain queries ($this->db->query($sql)), Database Query Builder ($this->from($table_name)->...->execute()) or any kind of AR (ORM, Sprig, Jelly - all of them allow you to set table_name property). Also you can use default (calculated from class name) table names as notJim described.
Underscores will be translated into directory separator, that's how kohana autoloading works.
But, that's not really matters as you can define $_table_name properties in your model.
class Model_Profile extends ORM
{
protected $_table_name = 'user_profiles';
}
Yes, you can. Because your model name is the table name with last word pluralized by default. I mean that
Model_Foo_Bar {
}
will be foo_bars table which Kohana will try to find by default.
Hey I have coded CakePHP for a number of things but never ran into this problem before surprisingly. Also I have thoroughly searched the net and CakePHP docs and have not found an answer to my question. My question is, I have a table for my model that should be named Class, obviously I cannot use that name though since it's a reserved PHP keyword. What options do I have to be able to refer to this model appropriately.
So far I have;
Renamed my class model file to player_class.php
Renamed my class model class to PlayerClass
Changed var $name to 'PlayerClass'
Added to my class model class; var $useTable = 'classes';
Renamed my class controller to player_classes_controller.php
Renamed my class controller class to PlayerClassesController
Changed var $name to 'PlayerClasses'
While this does work, is this what has to be done or are to other options to be able to refer to it as Class still, like can I do any sort of mangling like _Class?
I once tested all CakePHP class names for Cake 1.2 if they can be used as Model names, here are the results:
NOT possible is:
app
appcontroller
appmodel
behaviorcollection
cache
cacheengine
cakelog
cakesession
classregistry
component
configure
connectionmanager
controller
datasource
debugger
dispatcher
file
fileengine
folder
helper
inflector
model
modelbehavior
object
overloadable
overloadable2
router
security
sessioncomponent
set
string
validation
Possible is:
acl
aclbase
aclbehavior
aclcomponent
aclnode
aclshell
aco
acoaction
admin
ajaxhelper
apcengine
apishell
app_model
apphelper
aro
authcomponent
bake
baker
bakeshell
behavior
cachehelper
cake
cakeschema
cakesocket
consoleshell
containablebehavior
controllertask
cookiecomponent
dbacl
dbaclschema
dbconfigtask
dboadodb
dbodb2
dbofirebird
dbomssql
dbomysql
dbomysqlbase
dbomysqli
dboodbc
dbooracle
dbopostgres
dbosource
dbosqlite
dbosybase
element
emailcomponent
error
errorhandler
extracttask
flay
formhelper
htmlhelper
httpsocket
i18n
i18nmodel
i18nschema
i18nshell
iniacl
javascripthelper
jshelper
jshelperobject
l10n
layout
magicdb
magicfileresource
mediaview
memcacheengine
modeltask
multibyte
numberhelper
page
pagescontroller
paginatorhelper
permission
plugintask
projecttask
requesthandlercomponent
rsshelper
sanitize
scaffold
schema
schemashell
securitycomponent
sessionhelper
sessionsschema
shell
shelldispatcher
test
testsuiteshell
testtask
texthelper
themeview
timehelper
translate
translatebehavior
treebehavior
viewtask
xcacheengine
xml
xmlelement
xmlhelper
xmlmanager
xmlnode
xmltextnode
When i run into this sort of problem i usually do what you did, only i prefix the reserved word with "My" (so when i read the code it doesn't look like that class has anything to do with "Player"... for example, just the other day i wanted to model a "ACO" model.. but that already existed in cake (same scenario of reserved word) so i created a model called Myaco.
I think you should just name it Myclass.
Regarding the model name and controller name changes- i think you did good, i would do the same. Your only real option is to use the $useTable = 'classed'; to use your DB table.
If you use the underscore prefix, i believe cake will not be able to handle it (it will fail in the Inflector class).
Good luck
I can second that solution. I had the same problem and used a prefix that was the initials of the client. Ended up calling mine Dtclass. Unfortunately, it took me an hour or so to figure out what the problem was. One of those cases where the answer stares you in the face all the time till you finally recognize it.