Missing Table with Simpletest in CakePHP - php

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

Related

Use fixtures in integration tests

I found that CakePhp 3 use real db connection in integration tests, so line like $this->post('articles/delete/1') delete the real db row even if fixtures provided. Is it possible to use only data provided by fixtures (test connection)? Model tests are working normally with fixtures using test connection.
CakePHP performs the following during the course of a fixture based test case:
1. Creates tables for each of the fixtures needed.
2. Populates tables with data, if data is provided in fixture.
3. Runs test methods.
4. Empties the fixture tables.
5. Removes fixture tables from database.
So as mentioned in cake's documentation cakephp empties the fixture tables and no matter either it delete records or save records or do any other operation.
I found the reason for my problem.
In my bootstrap.php I had event listener binded to app events. In listener's constructor I initialized properties with models objects using TableRegistry::get().
The reason was that TableRegistry will remember connection to use on first call so that then ConnectionManager::alias() will have no effect. As I had TableRegistry initialization in my event's constructor it was called before main app loaded and fixtureManager added connection aliases.
So never call TableRegistry before app loaded or it may cause problems.

Difference between Woodling::saved and Woodling::retrieve

I am writing my unit tests with phpunit to Laravel application. I am using Eloquent and Woodling library. I want to test many to many relationship.
I have Users table and Friends table. Everything worked, when I tested it manually. I am able to add friends. I wanted to test this functionality.
I created blueprints and I call them with saved method like this.
$user = Woodling::saved('LonelyUser');
$user2 = Woodling::saved('LonelyUser2');
$users = User::all()->toArray();
var_dump($users);
$user->addFriend($user2);
I got a database constraint error in the last line, because the users were not persisted to database (I know that, because they are not in var_dump output).
If Woodling::saved does not persist to database, than what does it do? The docs say, that it calls save method on model. save method should persist the model to database.
What is the saved method purpose and how is it different than retrieve?
Woodling::saved persists to database correctly, but if something goes wrong during saving, it doesn't show any errors. In my case save method was actually Ardent::save, which does some validations. This validations did not pass and Model::save was never called.

Doctrine [Semantical Error] Error: Class xxx has no field or association named yyy

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

Renaming a MySQL table in a Zend/Doctrine PHP app fails?

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

Better use of models and migration in symfony

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.

Categories