Fetching textfield values from DB (Symfony5) - database

I’m in trouble and I need some help.
I’m currently working on Symfony 5.4 and I’m trying to make a dynamic landing page for a customer.
I wanted to make the landing easily editable for the customer so he doesn’t need to write inside the home.html.twig.
So I did a LandingPage entity with five textfields and five images.
I also did a LandingPageType, a LandingPageRepository and everything is controlled in the HomeController.
The thing is, I don't know how to fetch the textfield's DB values when editing with this type of configuration since there are no ID for each textfield.
Here's my edit function :
public function landingEdit(Request $request, LandingPageRepository $landingPageRepository): Response
{
$landingPage = new LandingPage();
$form = $this->createForm(LandingPageType::class, $landingPage);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$landingPageRepository->add($landingPage);
return $this->redirectToRoute('app_admin_panel', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('admin_panel/content/pages/landing.html.twig', [
'landing' => $landingPage,
'form' => $form,
]);
}
If someone has an idea, that'd be awesome !
Thanks in advance!

Related

Question Password reset token in Laravel Breeze Inertia React

I have a problem to use password reset view which demanding an token to create the view in laravel breeze combine with inertiajs and reactjs.
my question is :
Q1. Where is the token generated? or where do i retrieve the token?
//route
Route::get('reset-password/{token}', [NewPasswordController::class, 'create'])
->name('password.reset');
//controller
public function create(Request $request)
{
return Inertia::render('Auth/ResetPassword', [
'email' => $request->email,
'token' => $request->route('token'),
]);
}
as you can see this route demanding a token to create the view but i dont know how to generate the token. and i cant see any page access this route so there is no example about this.
Q2. How do i pass the token to controller
<Link method="get" href={route('password.reset')} data={{ token: token }} as="button">
Reset
</Link>
im planing to do like code above which i can click from user data grid based on user id so only admin can reset your password. or any better idea to implement this?.
thankyou in advance
If you installed breeze/laravel fresh installation you can simply generate the token using:
$token = $request->user()->createToken($request->token_name);
return ['token' => $token->plainTextToken];
What you need to make sure of is that you are using the trait HasApiTokens in the User Model:
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}

How to use $this->autoRender = false in cakephp 3.6.10?

I am trying to display the test data on the screen by running function test() in the pagesController. Used $this->autoRender = false for it, but it is still giving me error:
Please help me out. I think some version problem is there, but I can't figure it out. Thankyou.
Cakephp by default takes Pages controller's display actions as a home page. display function manages pages and subpages itself that is why you are getting error. Either you can change your default home page in your /config/routes.php
$routes->connect('/', ['controller' => 'Pages', 'action' => 'index']);
OR
define your test action in some other controller.
OR
Remove code from display action
class PagesController {
function display()
{
// default controller code here
// your custom code here
}
}
Hope this will work.

yii2 Getting array of data from one model and listing it in view of another

In photo controller while listing photo I can get user data using function
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'uID']);
}
So when I'm listing some photo in view I can access users db using
$model->user['username']
But what if I have db of comments of this photo. and want to get all coments while listing photo.
what should I write in photos model to get all comments of this photo and how can I list them in view ?
in model I think I must use this
public function getComments()
{
$modelComments = Comments::find()->where(['id'=>'commentID'])->all();
return [array('modelComments' => $modelComments)];
}
If this is correct how can I output all comments in view of photo ?
Does $model->user['username'] work at all? It should be $model->user->username.
Anyway set relation between photo and comments like with user but one-to-many.
public function getComments()
{
return $this->hasMany(Comments::className(), ['id' => 'commentID']);
}
Now when calling $model->comments you have got the array of all Comments models related to the photo.
Simpliest way of displaying them is to iterate over this array like:
foreach ($model->comments as $comment) {
echo $comment->content . '<br>';
// assuming column name with actual comment is "content"
}

CakePHP URL Parameter

How can I get the parameter in the URL and save it to a varialble so I can save it to my database?
example: www.mydomain.com/item/products/3 <-
This is for my upload image, so I can specify what product ID will I use for that image.
function add() {
if ($this->request->is('post')) {
$this->Upload->create();
if(empty($this->data['Upload']['image']['name'])) {
unset($this->request->data['Upload']['image']);
}
if(!empty($this->data['Upload']['image']['name'])) {
$filename = $this->request->data['Upload']['image']['name'];
$new_filename = STring::uuid().'-'.$filename;
$file_tmp_name = $this->request->data['Upload']['image']['tmp_name'];
$dir = WWW_ROOT.'img'.DS.'uploads';
move_uploaded_file($file_tmp_name,$dir.DS.$new_filename);
$this->request->data['Upload']['image'] = $new_filename;
if($this->Upload->save($this->request->data)) {
$this->Session->setFlash(__('Uploaded.'));
$this->redirect(array('action' => 'index'));
}
}
}
}
How will I add it here. Thank you in advance :)
Im using cakePHP
Just add the product id as a hidden input in the Form.
Then it will be included in the $this->data variable when you get the POST request.
For example in a controller method like the following
public function product($id) {
.....
}
You access it by the url (for example): www.mydomain.com/item/products/3 where Item is the controller, product is the method you are calling in that controller and 3 represent a parameter that is required to the function to work in this case $id. (assuming you don't have any routing configuration)
Is treated as a normal php variable, just do whatever you wanna do with it. Just make sure you pass the correct value

Cakephp - Pagination with JSON API

I am developing (with a partner) a CakePHP application which will use Backbone.js.
So, basically, my cake application largely behaves like a JSON API. After the controller action loads a certain view (no data is rendered yet), Backbone makes an AJAX call to another controller action to fetch the data. The controller return JSON.
(The routes.php file has the mapResources and parseExtensions line in place)
Controller code
public function index(){
$company_id = $this->Session->read('Company.id');
$channel_ids = $this->Order->Channel->find('all', array('conditions' => array('Channel.company_id' => $company_id), 'fields' => array('Channel.id')));
$channel_ids = Set::extract('/Channel/id', $channel_ids);
$data = $this->paginate('Order', array('Order.fulfillment_status_id' => 1, 'Order.channel_id' => $channel_ids));
$this->set('orders', $data);
//In case of a JSON request
if($this->RequestHandler->ext == 'json') {
$this->autoRender = false;
echo json_encode($data);
}
}
This worked fine, but we hit a wall when we tried to implement pagination with this.
How do we generate the Pagination links? (If we do it via Backbone, it does not know the total records in the database and hence does not know how many links to generate)
How should the "page number" be burnt into the AJAX call? How should it be deciphered in the controller?
Really stuck here, we will really appreciate your help.
In Your Controller
$this->layout = false;
$this->RequestHandler->respondAs('json');
$recipes = $this->paginate('Recipe');
$this->set(compact('recipes'));
In your View
echo json_encode($recipes);
echo json_encode($this->Paginator->params());

Resources