Wiki¶
Propel¶
When you checkout a server branch, you must install propel afterwards. This is done by going into the server root directory and running:
composer install
After your first installation of propel, you may want to create a symlink:
ln -s vendor/propel/propel/bin/propel propel
The configuration of the data base etc. is done in the config/propel.php
file.
(If this is your first time to install propel, you have to copy that file from somebody else. Make sure to adjust it to the DB you want to use.)
All the propel
commands will immediately take into account the new entry in config/propel.php
.
If your "ownserver", however should take this change into account, you have to run:
./propel config:convert
This will create the generated-conf/config.php
file, which is the actual configuration file used by the "ownserver".
Neither of the above files are monitored by git.
Therefore, the generated-conf/config.php
has to be manually copied or edited to any distribution like e.g. /var/www/smile/server/
(master/stable) and /var/www/smile/server2/
(develop/testing).
Normally, there are no changes needed, as the DB etc. does not change its name for develop
or master
.
Changing the DB structure¶
When you create a new table, make sure it has a Primary Key column!
Otherwise we have had some really weird behaviour with Propel
(Find-Queries returned the first result line as often as there were actual result lines...)
First of all, you should make a copy of the DB you want to make changes to in phpMyAdmin.
You then have to enter the name of that copy in the config/propel.php
file. (That file is not monitored by git.)
This is necessary, because you should not make changes directly to the real DB, but test them first with that copy.
Then make your desired changes in the schema.xml
file
and type
./propel diff
This will create a new mirgation file in the generated-migrations
directory.
E.g. PropelMigration_1453562886.php
.
This file should be carefully checked!
If eveything is OK, run
./propel migrate
This will apply the changes to your copied dummy database.
Try, if everything is still working.
To create the php classes (in generated-classes
) to access newly created DB tables, you have to run:
./propel model:build
Followed by:
composer dump-autoload
(For more details, see: http://propelorm.org/documentation/02-buildtime.html#generate-model-classes)
Then you can commit your changed schema.xml
file, the newly created migration file and the newly created files in generated-classes
.
After merging the server changes with the develop
or master
branch,
the DB changes have to be applied to the real develop
or master
DB. To do this, you have to
checkout the respective develop
or master
branch, set the respective real DB in your config/propel.php
and run:
./propel migrate
The following should not be necessary any more because of an automated "post update hook"...Then, somebody with admin (sudo) rights has to go into the respective folder /var/www/smile/server
(stable) or /var/www/smile/server2
(develop) or /var/www/smile/serverBetaTi
(beta) and run:
sudo su git
composer dump-autoload
Finally, the dummy DB can be deleted and your config/propel.php
file set back to its original state.
(Don't forget ./propel config:convert
)
For more details, see: http://propelorm.org/documentation/09-migrations.html
Renaming a DB table¶
FIrst make your desired changes in the schema.xml
file.
The generated migration, however, will want to drop the old and create a new table.
Manually replace the DROP and CREATE in the getUpSQL()
part of the genrated migration php file with:
RENAME TABLE IF EXISTS `new_table_name` TO `old_table_name`;
In the getDownSQL()
part, you do it the other way around:
RENAME TABLE IF EXISTS `old_table_name` TO `new_table_name`;
Custom SQL-Querys¶
http://propelorm.org/blog/2011/02/02/how-can-i-write-this-query-using-an-orm.html
$conn = Propel\Runtime\Propel::getConnection(); $sql = "SELECT :myValue ...."; $st = $conn->prepare($sql); $st->bindValue(':myValue', $someVariable); $st->execute(); $result = $st->fetchAll(PDO::FETCH_ASSOC)[0]["str"];
Select Queries¶
Select a single column with findOne:¶
$res = ChapterQuery::create() ->select('ParentId') ->filterById($previousId) ->findOne(); $desiredValue = $res;
Select several columns with findOne:¶
$res = ChapterQuery::create() ->select(array('Id','ParentId')) ->filterById($previousId) ->findOne(); $desiredValue1 = $res['Id']; $desiredValue2 = $res['ParentId'];
Select all columns with findOne:¶
$res = ChapterQuery::create() ->filterById($chapterId) ->findOne(); $desiredValue = $res->getPreviousId();
Checking if a query returns nothing!
In PHP5 times, you could always check for === NULL
, but since we switched to PHP7 you have to use different ways depending on how you obtained your results:¶
- findOne():
$res === NULL
- find():
$res->isEmpty()
- find()->toArray():
empty($res)
Show last executed SQL query:¶
trigger_notice(Propel\Runtime\Propel::getConnection()->getLastExecutedQuery());
Changin config of live server:¶
If you ever need to make changes to the config of the live server, you have to do this in the folder /home/git/gitDir/serverMaster
. This folder will automatically get copied to /var/www/repos/server/master
after each commit. So don't make your changes in the latter, because they will be overridden.