Elasticsearch Implementation on Cakephp 3 - elasticsearch

I was able to incorporate elasticsearch on cakephp 3 but I lost the ability to save on my database. The add method on the controller creates an index instead of doing both (save to table and create an index in elasticsearch). Followed the instructions from here - https://book.cakephp.org/3.0/en/elasticsearch.html.
Any idea on how I can achieve the above?
Thanks.
Adding code from Categories Controller.
public function add()
{
$category = $this->Categories->newEntity();
if ($this->request->is('post')) {
$category = $this->Categories->patchEntity($category, $this->request->getData());
if ($this->Categories->save($category)) {
$this->Flash->success(__('The category has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The category could not be saved. Please, try again.'));
}
$this->set(compact('category'));
$this->set('_serialize', ['category']);
}

Related

Laravel save new record returns nothing

I'm trying to save new record, all i get is a white page
even dump and dd for $applicant ->save() returns nothing.
public function store(Request $request) {
try {
if (Auth::guest()) {
return redirect()->route('login');
}
$applicant = new Applicant();
$applicant->first_name = $request->get('first_name');
$applicant->middle_name = $request->get('middle_name');
$applicant->last_name = $request->get('last_name');
dump($applicant);
$applicant->save();
dd('Saved');
} catch (Exception $e) {
Log::error("Error during orders creation:" .$e>getTraceAsString());
}
}
I expected the record should be saved
Try this instead
public function store(Request $request){
$applicant = new Applicant;
$applicant->first_name = $request->input('first_name');
$applicant->save();
}
and don't forget to include use App\Applicant model in your Controller class; If it does not solve your problem. please double check your form action post in view section.
I think save function will not return anything.
for data you must use create method.
Used return after save.
$applicant->save();
return response()->json(['success' => 'Data Added successfully.']);
Use create in stead, write less code if you can us always better.
Try this:
Application::create($request->all());
Or change it the way you need by assigning every array key to its value

Cakephp 3 does not work for the default language

Sorry for my english, but I hope you will understand me.
Simplified code looks like this:
//in bootstrap.php
ini_set('intl.default_locale', 'deu');
// MainMenusTable.php
public function initialize(array $config)
{
parent::initialize($config);
...
$this->addBehavior('Translate', ['fields' => ['title']]);
...
}
//in controller - THIS WORKS!
public function add()
{
I18n::locale('eng');
$mainMenu = $this->MainMenus->newEntity();
if ($this->request->is('post')) {
$mainMenu = $this->MainMenus->patchEntity($mainMenu, $this->request->data);
$this->MainMenus->save($mainMenu)
}
$this->set(compact('mainMenu'));
}
// in controller BUT THIS DOES'T WORK:
public function add()
{
I18n::locale('deu');
$mainMenu = $this->MainMenus->newEntity();
if ($this->request->is('post')) {
$mainMenu = $this->MainMenus->patchEntity($mainMenu, $this->request->data);
$this->MainMenus->save($mainMenu)
}
$this->set(compact('mainMenu'));
}
I have the same problem when I read the record
//in controller - THIS WORKS!
I18n::locale('eng');
$query = $this->MainMenus->find('all')->order(['MainMenus.id' => 'ASC'])->all();
// in controller BUT THIS DOES'T WORK:
I18n::locale('deu');
$query = $this->MainMenus->find('all')->order(['MainMenus.id' => 'ASC'])->all();
For 'deu' I manually entered records.
Do you know what the problem is?
Thanks!
This is a solution to the problem from https://github.com/cakephp/cakephp/issues/8416:
The behavior assumes that you store the records in the default language. If the current locale is the same as the default language, then it will just return the records in the database instead of fetching from the translations table.
The title will not be saved in the i18n table for the default language, that is only done for other languages.

Why redirect show blank page in model laravel?

I'd like to ask why the following code works, redirects normally, and data is successfully inserted :
CategoriesController :
public function store()
{
$data = Input::all();
$category = new Term;
if($category->saveCategory($data)){
return Redirect::route('admin_posts_categories')->withSuccess('Category successfully added.');
}else{
return Redirect::route('admin_posts_categories')->withError('Failed to add category. #ErrorCode : 13');
}
}
Term model :
public function saveCategory($data){
$this->name = $data['name'];
$this->slug = $data['slug'];
if($this->save()){
$category_taxo = new TermTaxonomy;
$category_taxo->term_id = $this->lastCategoryId();
$category_taxo->taxonomy = 'category';
$category_taxo->description = $data['description'];
if($category_taxo->save()){
return true;
}else{
return false;
}
}else{
return "#Error Code : 4";
}
}
Where as the following only inserts the data but then shows a blank page and doesn't redirect :
CategoriesController :
public function store()
{
$data = Input::all();
$category = new Term;
$category->saveCategory($data);
}
Term Model
public function saveCategory($data){
$this->name = $data['name'];
$this->slug = $data['slug'];
if($this->save()){
$category_taxo = new TermTaxonomy;
$category_taxo->term_id = $this->lastCategoryId();
$category_taxo->taxonomy = 'category';
$category_taxo->description = $data['description'];
if($category_taxo->save()){
return redirect::route('admin_posts_categories')->withSuccess('Category successfully added.');
}else{
return redirect::route('admin_posts_categories')->withError('Failed to add category.');
}
}else{
return redirect::route('admin_posts_categories')->withError('#Error Code : 4.');
}
}
Moreover, I'd like to ask a few related questions, does my code conform to correct design patterns, and where should I put the redirect, in the model or in the controller ?
Try this for redirect:
1. return Redirect::back()->withSuccess('Category successfully added.');
OR
2. return Redirect::to(URL::to('admin_posts_categories'))->withSuccess('Category successfully added.');
Add your redirect login inside Controller. Even if you want to put in model (which is not recommended) use Ardent Hook function i.e. afterSave().
First of all never put redirect logic in model. Models are for putting business logic. Second thing check whether you have created route for admin_posts_categories in route.php or not and how you are calling views. If possible post your route code in question.
I recommend not putting redirects in your model. So the first solution would be the best of the two you have.
But back to your problem. It is showing a blank page because your store function is not returning anything. return $category->saveCategory($data); but as previously stated this method is not best practise.
An excellent tip would be to have a look at Laracasts, this will teach you everything you knew, didn't know and more about Laravel.

Magento: create custom Controller

i have created one news module . it is working fine..
http://domain.com/magento/news
this page is displaying all the news items. with title, content and date.
i want to make view more link for content when user click on view more link it will redirect user to specific newsitem page
http://domain.com/magento/news/newsitem-1
i created another controller newsitemController.php with following code :
public function infoAction(){
$this->loadLayout();
$this->getLayout()->getBlock('content')
->append($this->getLayout()->createBlock('news/newsitem') );
$this->renderLayout();
}
also created block name info.php with below code:
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getnewsitem()
{
if(!$this->hasData('news')) {
$this->setData('news', Mage::registry('news'));
}
return $this->getData('news');
}
not getting output..
need help to get output.
In your info.php add the following function to get the url of the news item.
public function getItemUrl($newsItem)
{
return $this->getUrl('*/*/view', array('id' => $newsItem->getId()));
}
In your controller add following function to view the news detail page.
public function viewAction()
{
$model = Mage::getModel('magentostudy_news/news');
$model->load($newsId);
$this->loadLayout();
$itemBlock = $this->getLayout()->getBlock('news.info');
$this->renderLayout();
}
By doing this you can simply access the info page attaching this link on read more like
foreach ($this->getCollection() as $newsItem)
{
//Other Code
Read More..
}

CodeIgniter view problem

This is my first day playing with CI and I really like it so far but have a problem I can't solve on my own. The issue is that I need to generate single view with two controller functions. One div should include selected row by ID from table A and other div should loop foreach on array from table B.
public function index()//div A
{
$data['query'] = $this->db->get_where('beer', array('id' => 1));
$this->load->view('corp/corp_view', $data);
}
public function loadList() //div B
{
$data['q'] = $this->db->get_where('list', array('id' => 1));
$this->load->view('corp/mentor_list_view', $data);
}
I tried to solve this for few hours by creating another view for loadList() and then including it in the the main view like "$this->load->view()" but I'm getting the values from the index() function query table 'beer' not 'list' table as intended. Again I'm new to this and would appreciate your help.
Thank you for your help.
Thanks for the extra info, i can help yah out now.
In Codeigniter if you want to make a function that is not able to be called by the user just precede it with a '_'. So in your case:
public function index()//div A
{
$data['query'] = $this->db->get_where('beer', array('id' => 1));
$data['query2'] = $this->_mySecondQuery();
$this->load->view('corp/corp_view', $data);
}
public function _mySecondQuery() //div B
{
return $this->db->get_where('list', array('id' => 1));
}
Now in the index page you have access to both queries. Btw, I would not suggest doing much DB work in the controller. DB work is meant to be done in Models. For more info on those see: Codeigniter Models

Resources