Got question, maybe even problem while creating CakePHP Component. Basically, I need to implement few quesries that can be accesses everywhere in my layout (sidebar statistics and so).
When I try to query in Component, I got error about calling function on a non-object.
Damn, can anybody explain me this one?
Cheers!
Are you doing something like this?
class MyComponent extends object {
function startup(&$controller) {
$this->controller = $controller; // Stores reference Controller in the component
}
function common() {
$data = ClassRegistry::init('MyModel')->myQuery(); // Call the query on the model
$this->controller->set(compact('data')); // Sets data from myQuery in view
}
}
At the risk of sounding pedantic, you'd be violating MVC pretty egregiously by doing this. If you're okay with that, you can use App::import() to load any model from anywhere in your app (http://book.cakephp.org/view/531/Importing-Controllers-Models-Components-Behaviors-).
If you're interested in attempting to retain the MVC structure, we may be able to help with some more information about the queries you need to run in that generic manner.
Related
I have Mobx FirstStore and import SecondStore. SecondStore has a (value) and I want to use it (value) in my FirstStore.
As shown in the screenshots, this is how it works, but I have a question, is it safe to use it? If (value) changes in my SecondStore, will FirstStore see it?
Perhaps it is better to add a parameter and pass (value) through the React component when calling the function?
async someFetchRequest(valueFromSecondStore) {
await api.retData(valueFromSecondStore)
}
Thanks in advance!
======================================================================================
It is totally fine to use it like that in most cases.
There are cases when you might consider different approaches like Dependency Injection, or just pass values as parameters like you said. But until you encounter those advanced scenarios (make testing more approachable, for example, or server side rendering) you can safely use it like that. You can even make computed getter in the FirstStore, or reaction, with some value from the SecondStore and it will work as expected.
You can also encounter circular dependency loop in some rare cases if you import class A to class B and class B to class A, but if you only import them one way you are fine.
Im new to react framework. I'm facing an issue and need some help here.
An API call will result in an object which has several object rows.
I wanted to use map on that object to render the div's.
export default class SearchResult extends Component {
static propTypes = {
courses:React.PropTypes.object,
}
render() {
....
this.props.courses.map( entity => renderEntity(entity));
...
}
}
I'm having an error stating map is not a property. After searching in google i found object doesnt have map property. Only arrays have it.
Can some one help me
1) How do i get map functionality / equivalent. All I want to do is render a set of html tags for every element in the object. this is to develop a search result.
2) Can I convert object to arrays. Is that required.
3) if (this.props.courses) is always turning out to be true because it has proto initialised. In that case how will I check if courses really has data. courses.length etc didnt work as it doesnt have any property of length nor it is an array.
Object console log output
Note :- this is my first question in stackoverflow. So kindly ignore if you find any error.
Thanks in advance for help.
Sesh
More than a React problem this is a pure JS one. If your need is to cycle through the values of the object, then something like this should work just fine:
Object.keys(this.props.courses).map(course => renderEntity(this.props.courses[course]))
As a suggestion for future issues like this, always start looking here, in this specific case this, is the ideal like for your problem.
I have an API based on CakePHP, controller with name AccessLogController is responsible for saving access log into the database.
Question is:
What is the best practice for global logging in CakePHP?
I thought that I will call AccessLogController method from inherited AppController in before filter callback like this:
public function beforeFilter() {
$accessLogCtrl = new AccessLogsController();
$accessLogCtrl->add($param1, $param2);
}
But i not sure that is it a good way how to do it..
Many Thanks for any advice..
That's not how you should use controllers, never ever, except maybe in your test suite!
That being said, using AppController::beforeFilter() to globally log controller action requests is generally fine, just make sure that you always invoke parent::beforeFilter() in case you are overriding the filter in an extending controller.
However, you should definitely refactor your logging functionality into either a utility class, a component, a model, or even directly into AppController, depending on how it's actually logging things, and if you need to use it in places other than in AppController.
Using CakePHP, I am finding that I'm duplicating some code between controller actions. I have a dozen or so actions (belonging to various controllers) that all need to run the same query and set() the same 10 variables for the use in a particular layout. They also need to handle any errors in the same way and render an error page.
I know that components are intended to centralize logic used among controllers, but in my case, this logic needs access to the set() and render() methods of the controller. What is the suggested approach to this situation?
Thanks, Brian
Put the logic in your AppController class which your controller should extend from.
Check out the docs: http://book.cakephp.org/view/957/The-App-Controller
Ended up rolling my own sort of business logic layer on this one. Example below. Thoughts/comments welcome.
class MyController extends AppController {
public function my_action() {
// The BLL class is specific for this action and gets the entire
// controller so has access to the set() method as well as components.
$this->Bll = new MyActionLogic($this);
$this->Bll->do_whatever();
}
}
I got layout, nothing special, three columns, just to learn CakePHP. In documentation I found nothing about this case.
I got some statistics in sidebars, si I send them to layout file (default.ctp) cause they are displayed on every page.
I build (thanks to one user here) a component:
class SidebarComponent extends object {
function startup(&$controller) {
$this->controller = $controller; // Stores reference Controller in the component
}
function count_articles() {
$articles = ClassRegistry::init('Articles')->count_articles();
$this->controller->set(compact('articles'));
}
}
Everything is working perfectly. I got question about my technique. I needed to load component method in controller by putting:
$this->Sidebar->count_articles();
So I decided to make it a bit shorter, cause I will have to put it in every controller. So, I created new component's function:
function sidebars($userid) {
return array(
$this->top_articles(),
$this->random_article()
);
}
And I initialize it in controller that way:
$this->Sidebar->sidebars();
Everything is working correctly, I need only advice/feedback if it's good way I do this :)
Thanks for your time.
you don't need to call it explicitly in every controller. component's startup() method is called automatically before every action in the controller that is using your component.
so you can use startup() to get and set your data for views, and if you need some initialization before (like getting reference to controller), put it in component's initialize() method.
If you have sidebars with the same things in them, also sounds like a case for elements as well