I'm working on a website where people can download wallpapers.
I have a table 'Images' with a 'Download' column.
I would like to increment the value on this field for each time people click on the 'download button'
I'm usingg Symphony 6 with EasyAdmin-bundle & Twig
Can someone help me to make a query to increment this value ?
Thank you
enter image description here
<?php
namespace App\Entity;
use App\Repository\ImagesRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ImagesRepository::class)]
class Images
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $image = null;
#[ORM\Column(nullable: true)]
private ?int $downloads = null;
#[ORM\ManyToOne(inversedBy: 'images')]
private ?Wallpapers $wallpaper = null;
#[ORM\ManyToOne(inversedBy: 'device')]
private ?Devices $devices = null;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(string $image): self
{
$this->image = $image;
return $this;
}
public function getDownloads(): ?int
{
return $this->downloads;
}
public function setDownloads(?int $downloads): self
{
$this->downloads = $downloads;
return $this;
}
public function getWallpaper(): ?Wallpapers
{
return $this->wallpaper;
}
public function setWallpaper(?Wallpapers $wallpaper): self
{
$this->wallpaper = $wallpaper;
return $this;
}
public function getDevices(): ?Devices
{
return $this->devices;
}
public function setDevices(?Devices $devices): self
{
$this->devices = $devices;
return $this;
}
public function __toString(): string
{
return $this->image;
}
}
<div class="row">
{% for item in wallpapers.getImages() %}
<div class="col">
<img src="/uploads/wps/{{ item.image }}" class="card"/>
<button>Download</button>
</div>
{% endfor %}
</div>
I tried many example but as it's my first experience with Symphony is a bit complicated to understand all the concept of Query Builder
You must create a new controller with route like this:
#[Route('/downloads/{entity}', name:'downloads')
public function downloads(
Entity $entity,
EntityManagerInterface $em
){
$entity->setDownload($entity->getDownload() + 1);
$em->persist($entity);
$em->flush();
return $this->file($entity->getImage(), 'name_file.ext');
}
in your twig file you can redirect to this func with entity id param.
Function is an example you must use your entity class and get file content to download something.
I am having issue with the route and controller.
The error code consist of sql column not found which is looking for column id from items table. Which im quite curious due to the differences with my migration.
CartController.php
namespace App\Http\Controllers;
use App\Cart;
use App\CartItem;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CartController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function addItem ($itemNo){
$cart = Cart::where('user_id',Auth::user()->id)->first();
if(!$cart){
$cart = new Cart();
$cart->user_id=Auth::user()->id;
$cart->save();
}
$cartItem = new Cartitem();
$cartItem->itemNo=$itemNo;
$cartItem->cart_id= $cart->id;
$cartItem->save();
return redirect('/cart');
}
public function showCart(){
$cart = Cart::where('user_id',Auth::user()->id)->first();
if(!$cart){
$cart = new Cart();
$cart->user_id=Auth::user()->id;
$cart->save();
}
$items = $cart->cartItems;
$total=0;
foreach($items as $item){
$total+=$item->product->price;
}
return view('cart.view',['items'=>$items,'total'=>$total]);
}
public function removeItem($id){
CartItem::destroy($id);
return redirect('/cart');
}
}
ItemController.php
<?php
namespace App\Http\Controllers;
use App\Item;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
class ItemController extends Controller
{
public function index(){
$items = item::all();
return view('admin.items',['items' => $items]);
}
public function destroy($itemNo){
item::destroy($itemNo);
return redirect('/admin/items');
}
public function newItem(){
return view('admin.new');
}
public function add() {
$file = Request::file('file');
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put($file->getFilename().'.'.$extension, File::get($file));
$entry = new \App\File();
$entry->mime = $file->getClientMimeType();
$entry->original_filename = $file->getClientOriginalName();
$entry->filename = $file->getFilename().'.'.$extension;
$entry->save();
$Item = new Item();
$Item->file_id=$entry->id;
$Item->itemName =Request::input('name');
$Item->itemDescription =Request::input('description');
$Item->price =Request::input('price');
$Item->imageurl =Request::input('imageurl');
$Item->save();
return redirect('admin/items');
}
}
I am having problems with creating a schema / model for one of my projects and would like to get some help here.
I have 3 tables currently : accessories , products and a pivot table product_accessory
<?php
Schema::create('accessories', function(Blueprint $table)
{
$table->increments('id');
}
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
}
Schema::create('product_accessory', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('accessory_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('accessory_id')->references('id')->on('accessories');
}
Now problem is I need to add another product type 'adaptors' that would ultimately depend on the pivot table relation, that is, adaptors need to relate to both a product and accessory...
UPDATE
Here is how my current product_accessory_adaptor table is
Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_accessory_id')->unsigned();
$table->foreign('product_accessory_id')->references('id')->on('product_accessory');
}
This way, i can have many adaptors relating to a product and accessory. My question is how do i model this relation in eloquent?
Here's what i have now:
Custom pivot model :
class ProductAccessory extends Pivot {
protected $table = 'product_accessory';
public function product()
{
return $this->belongsTo('Product');
}
public function accessory()
{
return $this->belongsTo('Accessory');
}
public function adaptors() {
return $this->hasMany('Adaptor', 'product_accessory_id');
}
}
Product and Accessory model
class Accessory extends Eloquent {
public function products()
{
return $this->belongsToMany('Product', 'product_accessory', 'accessory_id', 'product_id')->withPivot();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
if ($parent instanceof Product) {
return new ProductAccessory($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
public function adaptors()
{
return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'accessory_id', 'product_accessory_id');
}
}
class Product extends Eloquent {
public function accessories()
{
return $this->belongsToMany('Accessory', 'product_accessory', 'product_id', 'accessory_id')->withPivot();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
if ($parent instanceof Accessory) {
return new ProductAccessory($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
public function adaptors()
{
return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'product_id', 'product_accessory_id');
}
}
Adaptor model:
class Adaptor extends Eloquent {
protected $table = 'product_accessory_adaptor';
public function productAccessory() {
return $this->belongsTo('ProductAccessory');
}
}
Update
Now the schema and model is setup. However there are issues with using hasManyThrough relations. In addition, any way to do eager loading in this case for the pivot relation , i.e Adaptor?
Note
The error that occurs when i make a call to adaptors() on either the Product or Accessory model is
Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model, none given, called in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 872 and defined
That's your pivot:
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
// you don't need to call it ..Pivot, just my suggestion
class ProductAccessory extends Eloquent {
protected $table = 'product_accessory';
public function product()
{
return $this->belongsTo('Product');
}
public function accessory()
{
return $this->belongsTo('Accessory');
}
public function adaptors()
{
return $this->hasMany('Adaptor', 'product_accessory_id');
}
}
// Product model
public function adaptors()
{
return $this->hasManyThrough(
'Adaptor', 'ProductAccessoryPivot', 'product_id', 'product_accessory_id'
);
}
// Accessory model
public function adaptors()
{
return $this->hasManyThrough(
'Adaptor', 'ProductAccessoryPivot', 'accessory_id', 'product_accessory_id'
);
}
Now example usage:
$product = Product::first();
$product->adaptors; // collection of all adaptors for given product
$product->adaptors->first()->accessory; // accessory for single adaptor
$product->accessories; // collection of accessories, each with your custom pivot, so:
$product->accessories->first()->adaptors; // collection of adaptors for given product-accessory pair
... and more, try it
I try to get number of rows per query in my wordpress Class but I get each time null, what is wrong with the following
class MyClass{
private $wpdb;
private $query;
public function __construct(){
global $wpdb;
this->wpdb = &$wpdb;
}
public function build_query(){
//dynamic query will be build here, example
$this->query = 'SELECT id,name From my_table';
}
public function get_display_result(){
$res = $this->wpdb->get_results($this->query, ARRAY_A);
return $res;
}
public function get_total_results(){
$res = $this->wpdb->get_results($this->query);
$nr = $res->num_rows;
var_dump($nr);// I get NULL
exit();
return $res;
}
}
You have to use $wpdb to get number of rows
$this->wpdb->num_rows;
I am using Cakephp 2.3.0, loading following component.
class BreadCrumbsComponent extends Component {
public $components = array();
public $controller = null;
public function initialize($controller) {
}
public function startup($controller) {
$this->controller = $controller;
}
public function beforeRender($controller) {
}
public function shutDown($controller) {
}
public function beforeRedirect($controller, $url, $status = null, $exit = true) {
}
public function handle($controllerName = NULL, $actionName = NULL) {
pr($this->controller->modelClass);
}
}
It get error following error
Trying to get property of non-object [APP\Controller\Component\BreadCrumbsComponent.php, line 38]
I am unable to access $this->controller there. Any reason? How do I make it work?
Read here startup method is called after the controller so need to initialize controller in initialize method as below,
public function initialize(&$controller, $settings = array()) {
$this->controller = $controller;
}