GithubHelp home page GithubHelp logo

thecodingmachine / database.tdbm Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 25.0 1.8 MB

The Database Machine is a PHP ORM that requires no configuration. The object model is deduced at runtime from the database model.

PHP 100.00%

database.tdbm's People

Contributors

camillekoppel avatar cbalda avatar charlesc-tcm avatar dmaechler avatar fooriva avatar harikt avatar homersimpsons avatar lebacql avatar moufmouf avatar nguyenk avatar nipatcm avatar npeguin avatar quentindubes avatar vaidiep avatar xhuberty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

database.tdbm's Issues

protect reserved keywords

when some columns are reserved keywords, update / insert queries may break :

$bean->setOrder(1);
$dao->save($bean);

will throw a SQL Syntax error

Add a `sort` method in the ResultSet

It would be great to be able to sort a column from the ResultSet. This requires pushing the logic of generating SQL in the ResultSet object (which should be possible).

foreign key set to null

In a BaseBean file:

if (!$stopRecursion) {
$array['parent'] = $this->getParent()->jsonSerialize(true);
}

need to check if the Parent is null

Add support for JSON columns

It would be very nice if JSON columns return a stdClass instead of a string (a bit like datetime SQL type already returns a DateTimeImmutable PHP Type)

Error join table FK

When a foreign key points towards a join table. (With no other attribut than id and foreigne key), it's doen't work. Since this join table is not related to a beans.

Rename egt/setters by object for foreignKeys

Ex: a User Bean that has embedds a City one (user.city_id). This will generate the following setters/getters :

 * get/setCityId(int $cityId);
 * get/setCity(CityBean $city);
 * get/setCityByCityId(CityBean $city);

The last two setters are named according to the referenced table (city), which is nice, but confusing when there is more than 1 FK pointing to the same table. Ex : user.city_id and user.birth_city_id, and also generates very long method names.

Would it be possible, ini next version to make something like that :

 * get/setCityId(int $cityId);
 * get/setBirthCityId(int $birtjCityId);

 * get/setCity(CityBean $city);
 * get/setBirthCity(CityBean $city);

... by looking for id_... or ..._id patterns in foreign keys columns. If those patterns are not found, then the accessors could just be named by the column (camelcase) name.

empty child object will not be persisted

When creating a new ChildBean, the Dao::save() function will not insert the line in child_table.

This seems to be due to the fact that there is no dbRow field pointing to the child_table.

Unable to install composer require mouf/database.tdbm

When I run composer require mouf/database.tdbm it gives error:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for mouf/database.tdbm ^4.2 -> satisfiable by mouf/database.tdbm[v4.2.0].
    - mouf/database.tdbm v4.2.0 requires mouf/database.doctrine-dbal-wrapper ~1.1 -> satisfiable by mouf/database.doctrine-dbal-wrapper[1.1.x-dev] but these conflict with your requirements or minimum-stability.


Installation failed, reverting ./composer.json to its original content.

Should it not install dependencies itself?

Order by clause on multiple tables

The order by is not possible for a field which is not in the select tables clause (when we have a request containing multiple join clause). I have this error :
"SQLSTATE[HY000]: General error: 3065 Expression #1 of ORDER BY clause is not in SELECT list, references column '..' which is not in SELECT list; this is incompatible with DISTINCT"

Add support for php-weak-lib

ext/weakref has not been migrated to PHP 7.
There is this new lib: "php-weak-lib"

It could be a good idea to migrate the old "weakref" code to the new "php-weak-lib" code.

[TDBM 4] Unclear error message when a table name is wrong.

Let's assume this code:

public function getUsersByManagerId($managerId)
    {
        return $this->find('contacts.manager_id = :id!', ['id' => $managerId]);
    }

If the table 'contacts' does not exists (maybe its 'contact' instead), we get this error message:

1) Mouf\Database\TDBM\TDBMDaoGeneratorTest::testQueryNullForeignKey
Fhaculty\Graph\Exception\OutOfBoundsException: Invalid vertex ID

We definitely need to improve the error message (and maybe even suggest the right table name).

TDBM4: this code fails

Run on the test database

$personDao = Mouf::getPersonDao();

// Let's get the list of users
$personList = $personDao->findAll();

// Let's display the names
foreach ($personList as $person) {
    /* @var $person PersonBean */
    echo $person->getName()."\n";
}

unstable DECIMAL type

In dependant doctrine library, a DECIMAL column is now bound to php type string. Therefore, retrieving such a column will return a string, ie a number properly formatted. However, once the column has been set, a float will be returned as long as the object is cached. Future queries will return a string again.

Example

// table `foo` containing column `bar`: DECIMAL(10,2)
$fooBean = $fooDao->getById(1);
echo $fooBean->getBar();
// "4.20"
$fooBean->setBar(4.1);
echo $fooBean->getBar();
// 4.1
$fooBean->setBar("4.10");
echo $fooBean->getBar();
// 4.1

The issues inferred are both an approximative precision and an unstable number format.

(cd doctrine/dbal: 9f51afa2)

Edit: typing error corrected

Cache in DAO generation

Sometimes, there seem to be a cache on data model while generating the DAOs.

After a cache clear, everything works fine...

Wishlist for TDBM 4.0

List of features that would be great for TDBM 4.0:

  • Simple beans (do not extend TDBM_Object)
  • Based on Doctrine DBAL (let's deprecate DBConnection)
  • Autogenerated "findBy..."
  • Automated cascading delete
  • Replace timestamps by DateTime
  • Inheritance support
  • Ability to have beans inside a third party package, and that beans should be extendable.
  • Autodetecting pivot tables
  • Ideally: possibly having several backends (MySQL, MongoDB, etc...)
  • Beans with ability to get related tables (e.g. $contact->getAddresses() )

Code rendering issue

Hi,

Was going through the documentation https://github.com/thecodingmachine/database.tdbm/blob/4.2/doc/quickstart.md#discarding-parameters-automatically-with-magic-parameters and noticed some code ( Just above paragraph of the link provided ) like

php // NEVER DO THIS! $list = $this->find("name LIKE '".$firstLetter.'%"' );

The code is actually written inside div. So I am not sure if that can be fixed or it is using a different approach to show on website.

Just thought of bringing it here for I wasn't able to fix it .

PSR-2 compliance

The generated DAOs and Beans are not fully PSR-2 compliant.

We should:

  • generate DAOs and beans that are PSR-2 compliant
  • change unit tests to add PHP-CS-Fixer testing to ensure the DAOs and Beans are PSR-2 compliant

Note: by default, PHP-CS-Fixer ensures PSR-2 + an additional set of rules. We are only aiming for strict PSR-2 compliance (no additional rules)

Database - getObject from one table and one joint table

Table:
1, Department (id, label)
2, Beneficiary (id, department_habitation_id)
3, Mobility_department_beneficiary (beneficiary_id, mobility_department_id)

Idea:
We need to get DepartmentBean from table Beneficiary via foreign key "department_habitation_id",
and we could also get BeneficiaryBean and DepartmentBean from table Mobility_department_beneficiary via his two foreign keys.

Problem:
In generated DepartmentBaseBean, we got two functions with a same name, getBeneficiary():
` /**
* Returns the list of BeneficiaryBean associated to this bean via the mobility_department_beneficiary pivot table.
*
* @return BeneficiaryBean[]
*/
public function getBeneficiary() {
return $this->_getRelationships('mobility_department_beneficiary');
}

/**
* Returns the list of BeneficiaryBean pointing to this bean via the department_id column.
*
* @return BeneficiaryBean[]|AlterableResultIterator
*/
public function getBeneficiary() : AlterableResultIterator
{
return $this->retrieveManyToOneRelationshipsStorage('beneficiary', 'fk_beneficiary_2', 'beneficiary', [ 'beneficiary.department_id' => $this->get('id', 'departments') ]);
}`

About documentation: Advanced tutorial

In the section "More about the filter bag" there are bad syntaxes or old syntaxes:
$france = $this->getCountryListByFilter("countries.country_name='France'"); for "->"
new TDBM_EqualFilter replaced by new EqualFilter
new TDBM_OrderByColumn replaced by new OrderByColumn
It could be interesting to add an example about auto JOINING.

About documentation: Quick start guide

In the Quick start guide documentation we speack about tdbm in Mouf, and we begin with an example using "Mouf::getUserDao()". To increase Mouf understanding, I think it could be interesting to always bind an instance of the dao or of the daoFactory in examples.

Add support for Weakrefs

In order to improve drastically memory consumption (for scripts), we would need to add support for weakrefs so that TDBMObjects referenced in the TDBMService are automatically freed if noone is referencing them.

About documentation: Generating DAOs

Images are not up to date.
I think some functions are deprecated (or removed), for example getNew() replaced by create() in "The DAOs structure".

Add support for generated columns

Generated columns are the result of an automated calculation or JSON extraction (in mysql, but they also see to exists in MsSql).
Problem is, they are loaded into the dbRow and therefore will be added for update or insert statements, which will trigger an error.

Note : it would be nice to keep the possibility to get the value :)

Fix message when there is an sql syntax error

If you make an error of syntax, example:
date_start <= :endDate AND date_end => :startDate
The date_end must be date_end >= startDate

The message is Call to a member function walk() on null in vendor\mouf\magic-query\src\SQLParser\Node\AbstractTwoOperandsOperator.php150
Not really expressive

Fatal: Cannot use object of type Generator as array

TDBM version: v3.3.0

I did a script that use the cursor mode for the fetch mode of tdbm service.

  1. I get a list of objects with "$objects = \Mouf::getObjectDao()->getList();"
  2. Then I use a foreach on this Generator to get each object inside $object.
  3. I call my function with one object inside this loop: "\Mouf::getScoringService()->calculateGlobalScore($object);"
  4. Inside this function I get another object of type "salle" thanks to the $object: $this->salle = $this->daoFactory->getSalleDao()->getByObject($this->object)
  5. The function getByObject is fired, here is the code:
    public function getByObject($object) {
    return $this->getByFilter(new EqualFilter('salles', 'object_id', $object->getId()));
    }
  6. And then, TDBMService is called and fail at this point:
if ($isFilterBag == true) {
    // If a filter bag was passer in parameter, let's perform a getObjects.
    $objects = $this->getObjects($table_name, $filters, null, null, null, $className);
    if (count($objects) == 0) {
        return null;
    } elseif (count($objects) > 1) {
         throw new DuplicateRowException("Error while querying an object for table '$table_name': ".count($objects)." rows have been returned, but we should have received at most one.");
    }
    // Return the first and only object.
    return $objects[0]; // FATAL ERROR HERE
}

Here is the stacktrace:
PHP Fatal error: Cannot use object of type Generator as array in /home/guillaume/projects/abcsalles_admin/vendor/mouf/database.tdbm/src/Mouf/Database/TDBM/TDBMService.php on line 369
PHP Stack trace:
PHP 1. {main}() /home/guillaume/projects/abcsalles_admin/src/scripts/calculate-object-score.php:0
PHP 2. ABCSalles\Services\ScoringService->calculateGlobalScore() /home/guillaume/projects/abcsalles_admin/src/scripts/calculate-object-score.php:20
PHP 3. ABCSalles\Dao\SalleDao->getByObject() /home/guillaume/projects/abcsalles_admin/src/ABCSalles/Services/ScoringService.php:51
PHP 4. ABCSalles\Dao\SalleDaoBase->getByFilter() /home/guillaume/projects/abcsalles_admin/src/ABCSalles/Dao/SalleDao.php:1454
PHP 5. Mouf\Database\TDBM\TDBMService->getObject() /home/guillaume/projects/abcsalles_admin/src/ABCSalles/Dao/SalleDaoBase.php:130

I guess that "$objects[0]" fails because its type is Generator instead of array. And I guess that its type is Generator because "$objects = $this->getObjects($table_name, $filters, null, null, null, $className);" is called with cursor mode, so a Generator object is returned.

TDBM

Add mapping between tables and beans' names which allows users not to specify className in request like tdbmService->getObjectsFromSql()

Seems lazy loading is not working...

Example :

$users = $this->getListByFilter(...);
var_dump($users);

--> gives an empty TDBM Array even if SQL returns rows

Seems to be caused by broken lazy loading because when trying to access $users[0], IDE inspector returns a UserBean and var_dump is the expected...

Recursion detected

Hello,

after few attempts if found there was recusion on objects.

The Dao of the project i work on is not from me but supposing it is a nice programmed Dao i may use bad my objects?

In fact in my controller I do the following:

$this->something = $this->daoFactory->getSomething Dao()->getList();

and in my view when I print_r($this->something) php detect recursion.

Do I do Something wrong?

Add a naming strategy

In 4.0, each bean class name ends with "Bean". For instance: UserBean.
Since the beans can be used as pure objects, it could be great to have a class names User only.

In order to not wreak havoc, the best idea would be to build a naming strategy class/interface for beans and daos.

SQL injection in OrderBy clause

The $orderBy parameter of findObjects may offer a vector of attack from SQL injection. It may be obvious, but most people don't realize this. TDBM should definitely check that parameters passed to orderBy in findObjects and findObjectsFromSQL are only colrefs.

In order to input an expression, we should require users to tell they know what they are doing (for instance by using a "new DangerousOrderBy('RAND()')" method.

Serialize a bean

Nice to have, add the option to serialize in php the bean object.
(Very interesting for the right plugin ;-) )

setElements() method

In the case of a many-to-many relationship, it would be nice to have a set method which would take an array of beans as parameter and would compare with the array of already linked elements (remove beans not in the parameter array and add beans not already linked).

TDBM 3.3 : infinite loop if table not exists

In a LessFilter, if you pass a table name that doesn't exists, the method getListByFilter leads to an infinite loop.

I think that you must test before if the table exists and thus, throw an exception if not.

Problem when json_encoding an interal TDBMObjectArray

When I get a TDBMObjectArray, and then ad it into a object I want to json_encode I get the following error :

Modification of ArrayObject during sorting is prohibited in D:\projets\ABCSalles\work\abcsalles\vendor\mouf\database.tdbm\src\Mouf\Database\TDBM\TDBMObjectArray.php on line 175

Unit test has been added : TDBMServiceTest::testTDBMObjectArrayInnerJsonEncode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.