I have a PHP backend application which is used both by an administration interface and a game. I know my way around PHP but I'm not familiar with Zend and Doctrine.
There's just two tables in MySQL (for schools and for students) and I want to change their name prefixes, because they refer to an earlier release of the game.
In short: I renamed the tables, I modified all PHP references to use the new names. There is not a trace of the old name anymore in all the Zend and Doctrine folders. But now the web application fails because it is still looking for the old names.
Here's the error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db1.neverland_schools' doesn't exist
So what happened is I changed the table name from neverland_schools to adventure_schools. It still looks for the old table name.
What I suspect is this being cache. But I don't have command line access to the server, nor experience with talking to Doctrine via command line.
If cache is the problem, would there be a way to reset the cache from PHP?
And if something else is the problem, I'd love to know.
Thanks!
If you have base class defined You must change name of schools and students base class. Path for base classess is in your config, or bootstrap file.
If not have you tried setting the table name explicitly, as in the first line
of the method below?
class Adventure_Schools extends Doctrine_Record {
public function setTableDefinition () {
$this->setTableName('adventure_schools');
...
You can delete the cache data :
you can emoty the tempory folder of the server
delete Doctrine cache
$deleted = $cacheDriver->deleteAll();
Maybe you should empty the proxy folder also.
You can verify that you changed the table name in the metadta depending on the driver.
if you are using the yaml or xml maybe the plin text serch doesn't take in ccount the file formt.
anotation
// entities/Product.php
/**
* #Entity **#Table(name="products")**
**/
xml
<entity name="Product" table="products">
yaml
# config/yaml/Product.dcm.yml
Product:
type: entity
table: products
Related
if not,there will be an error:
Column 'xxxx' doesn't make part of the column map
i can't find any information to solve this problem
You have two options:
You can update array with column map in method columnMap() to include changes in database table column names.
You can remove the method columnMap() from Model class - this will disable checking if columns exist in database table, allowing You to ignore newly added fields. Changes to existing table columns can break existing code.
If You have control over the database schema, then use the first method, as this will prevent errors like only some of the database queries not working. Otherwise use the second method.
Here is a helpful link to Phalcon documentation on column mapping.
Phalcon comes with devtools which is a great command line tool to automate tasks like creating models and controllers. I'd recommend that you install this and generate model using command like phalcon model MODELNAME. Otherwise, you will have to manually change the model names in the columnmap located in the model class.
For last few days I'm experiencing following issue with doctrine - since I am not allowed to paste any source code, I'll try to describe briefly:
I am using doctrine orm and I need to add a new column to an existing table in DB - mapping between DB and entities is done via xml mapping file - here are the steps I've proceeded:
I've added into the entity file - let's call it Entity.php - new field for that newColumn
I've added info about this newColumn into the XML mapping file as new XML element 'field'
I've executed doctrine command to change the schema of the DB based on edited mapping file
I've updated the query in EntityRepository.php file to include this new column.
When I then run the application, I am still getting this error:
[Semantical Error] Error: Class Entity.php has no field or association named newColumn
So, if I am understanding this properly, it is saying that in the Entity.php is not field newColumn to which should be the new DB column mapped.
But that is not the case, since it was the very first step I've done.
I've already tried this:
Checked there is no typo in name of the newColumn across all files
Checked the field in Entity.php has proper access modifiers - i.e. it is not private
Cache was cleared for the case that some bad version of Entity.php was stored
I've restarted apache server which the application runs on
Check your metadata cache. If you're using some external caching mechanism (like Memcached or xcache) it's probably shared across your vhosts. If one vhost populates the cache with its own mapping metadata (after apache restart), second vhost just uses it and doesn't care about different .dcm.xml mappings.
If it's your development server/vhost, it's usually much better to disable ORM caching at all (config/autoload/database.local.php).
Maybe my problem and solution would help somebody.
/* *
* #ORM\Column(name="user_id")
*/
protected $userId;
No, there was no typo in variable name. Access is correct and everything looked to be fine.
Some of you probably already see the problem.
Yes. It's /* * instead of /**. I took me about hour to find it :]
What was strange - it was working in join, but not when I have used it in where.
run this command
php bin/console doctrine:cache:clear-metadata on both APP_ENV=prod and APP_ENV=dev
Have the same problem. The solution was to replace in query condition like
LOWER(l.bank_title) LIKE :search
with its camelCase variant:
LOWER(l.bankTitle) LIKE :search
I'm looking for ideas to properly handle my project's mysql table updates across environments. I've taken a look at the CI DB Forge class and I believe this might help me out a bit. My thoughts are to:
create a new file for each database install, table upgrade or change. The file would contain the raw mysql query to do each relevant task
run the upgrade scripts via hooks before any controllers are loaded
continue loading the project
Is this the correct thinking? This is pretty similar to how Magento handles database upgrades per extensions.
Sounds like you are looking for the Migrations class. This is a fairly new library, and the documentation at this moment is not too good in my opinion.
If you enable this library in application/config/migrations.php and load it, the it will create a database table called migrations. The workflow from there is the following:
Create a new file under application/migrations make sure you name it with sequentially numbered file name like 001_some_descriptive_name.php. The format is important, exactly 3 numbers and at least one _ after them.
In the new file create a class named after the file name, so the 001_some_descriptive_name.php should hold a class called Migration_Some_descriptive_name and extend the CI_Migration class. The class name casing is important, first Migration_ then one uppercase letter then lowercase.
Create a public up and a public down method inside the class
Inside the up method add your migration code, that changes the database. You can use the db forge library or just plain old $this->db->query() calls. Dbforge is more portable if you need to support multiple database systems its probably better to use that.
Inside the down method add the code that would reverse the effects of up. If up adds a column then down should drop that column, if up creates a table, down should drop that table and so on.
Once you finished your migration class bump the migration_version inside the migration config file.
Create a controller load the migration library and call $this->migration->current() this will check the version from the migration database table and run the migration classes up or down methods in order to reach the migration version in the config file you set at step 6. So for example if the database says you are at version 2, and the config says you should be on 5, then it will run the up method of the migrations with 003_..., 004_..., 005_.. filenames in order. If you set lower number then the current the down methods will be called. The database starts the counting from 0 so don't create a 000_... file.
If you feel adventurous you can create a hook that loads the migration class and runs the get_instance()->migration->latest() on every page load so every environment will auto update the db once you deploy a new migration class.
Hey.
I'm having a hard time migrating changes I've done i my config/doctrine/schema.yml file.
I added the column age to the user table. Then I did a php symfony doctrine:generate-migrations-diff followed by php symfony doctrine:migrate .
Looking in my database, the column age is now added, without deleting any data.
But, my /lib/model/doctrine/base/BaseUser.class.php is not changed, there is no age field or functions for age . So I also did the command php symfony doctrine:build-model . Finally the model is updated/migrated too.
So I wonder, is this the only way? Seems like a lot of work, and I'm afraid to miss something each time doing it.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
This might be a bad question, but why is the model layer inside the /lib/doctrine path? As far as I have learned, you keep modules inside apps, where you create your view and controller. Why should the model be outside. Like this I can make models without attached controller and view?
Thanks.
Why should the model be outside
Because models can be used everywhere in your project, in example, in different applications and modules.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Of course you can, but migrations are a good approach to track your schema when deploying to production or working in team.
Here how I use doctrine migrations (simple use-case):
Add a column age to my User model in schema.yml
./symfony doctrine:generate-migrations-diff. Migration class(-es) have been generated.
./symfony doctrine:migrate. Column age successfully added to table.
./symfony doctrine:build --all-classes. Build forms/filters/models
That's it. The main idea is that doctrine:generate-migrations-diff class:
Gathers information about all your models' structure (php-representation of schema.yml)
Compares your schema.yml and info from (1)
Generates migration classes based on difference
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
Yes, you can add such method to User model because it's about users.
I'm using cakePHP and am using Simpletest as the testing suite. Whenever I run tests on the models, I get an error:
Missing Database Table
Error: Database table account_types for model AccountType was not found."
(For whatever)
Does anyone know how to fix this problem?
My guess is the fixtures are not being created or something along these lines.
Found the answer to my particular problem. In the actual test case files (mine was in app->tests->cases->models) the fixtures used were not auto-generated into the $fixtures variable.
The simple solution to this was whenever a "Missing Database Table" error comes up, I would make sure I put the name of the database not found (the actual fixture) in the $fixture variable in the test file.
So lets say that account_types was not found. In the actual test case I was running, where the $fixtures variable was, I would do:
var $fixtures = array('whatever_fixtures_where_already_here', 'name_of_missing_fixture', 'name_of_another_missing_fixture');
All fixtures that you will be using directly must be in the fixtures array and there also have to be fixtures created for every model related to (hasMany, belongsTo, etc) the fixtures in the fixture array