0

I'd like to override a function that exists in the CakePHP 3 core (to be more specific Cake\Database\ValueBinder).

How can I achieve this? I tried copying the class to src/Database/ and changed the namespace from Cake to App. I use'd the class in AppController, but had no luck.

Thanks in advance

Adrian

boindil
  • 95
  • 1
  • 9
  • 1
    To me this sounds as if you don't really know what you are actually trying to do. You'll most probably get better help if you'd describe the actual problem that you are trying to solve by "overriding a core class" – ndm Mar 08 '16 at 20:45
  • @ndm thanks for your answer, but I'm having a use case where I do need to override this specific class. Sadly your answer does neither help nor answer my question. – boindil Mar 08 '16 at 22:33
  • 2
    I'm pretty sure that you don't actually need to do that (custom/extended value binders can be assigned to query objects out of the box already). However, in any case you should explain your use case, in order for anyone to be able to give you a proper answer. – ndm Mar 08 '16 at 22:39
  • @ndm well that sounds more like it. I did not find an option to use own value binders in the CakePHP Book / API. How to do this? Can I set my own value binder as the default value binder? – boindil Mar 09 '16 at 00:03
  • 1
    Yes you can, by creating an extended Query class with an overriden valueBinder() method. The custom query class can be made the default by overriding Table::query(). If you need the lower level access to be affected too, you'd need to create (and use) an extended datasource Connection class and override Connection::newQuery(). – ndm Mar 09 '16 at 10:55
  • Hi, thanks for your comment. This solves it for me. Stupid me I didn't think of overriding it in my AppTable that i use for everything... However I didn't know of Connection::newQuery(), that's very useful! Would you like to put this into an answer, so I can accept it? – boindil Mar 09 '16 at 12:04

1 Answers1

0

ndm helped me a lot with this (see comments to my question), but since he didn't post an answer I am doing this now.


It's possible to use an own ValueBinder by setting it on my query: $this->Table->find()->valueBinder(myOwnValueBinder).

To make that the default ValueBinder in my own code (sufficient in most cases) one can use e.g. an AppTable as known from CakePHP 2 that extends to Table and make all other *Table classes extend AppTable. Now one only needs to create the following method:

public function query() {
    return parent::query()->valueBinder(myOwnValueBinder);
}

In case it's needed that all code uses teh custom ValueBinder, an extended datasource Connection class has to be created (and used). In this class Connection::newQuery() has to be overriden.

boindil
  • 95
  • 1
  • 9