proceed further with beans
This commit is contained in:
		
							
								
								
									
										29
									
								
								Update.php
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								Update.php
									
									
									
									
									
								
							@@ -10,39 +10,26 @@ class Update
 | 
			
		||||
{
 | 
			
		||||
    use Conditionable;
 | 
			
		||||
 | 
			
		||||
    protected Schema $table;
 | 
			
		||||
    protected Schema $schema;
 | 
			
		||||
    protected array $values = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(Schema $table) {
 | 
			
		||||
        $this->table = $table;
 | 
			
		||||
        $this->schema = $table;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets a new value for a column.
 | 
			
		||||
     * @param string $col Column name.
 | 
			
		||||
     * @param mixed $val Value to set.
 | 
			
		||||
     * @return self Current instance for method chaining.
 | 
			
		||||
     * @throws InvalidArgumentException
 | 
			
		||||
     */
 | 
			
		||||
    public function set(string $col, mixed $val): self {
 | 
			
		||||
        $this->table->requireColumn($col);
 | 
			
		||||
 | 
			
		||||
        $columnType = $this->table->columnType($col);
 | 
			
		||||
        $castedValue = $columnType->dbCast($val);
 | 
			
		||||
        $info = $this->schema->cols[$col];
 | 
			
		||||
        $type = $info->type;
 | 
			
		||||
        $castedValue = $type->dbCast($val);
 | 
			
		||||
        $this->values[$col] = $castedValue;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Generates the SQL statement.
 | 
			
		||||
     * @return string SQL query.
 | 
			
		||||
     * @throws InvalidArgumentException
 | 
			
		||||
     */
 | 
			
		||||
    public function toSql(): string {
 | 
			
		||||
        if (empty($this->values))
 | 
			
		||||
            throw new InvalidArgumentException("No values have been set for the Update.");
 | 
			
		||||
 | 
			
		||||
        $table = $this->table->name;
 | 
			
		||||
        $table = $this->schema->name;
 | 
			
		||||
        $setParts = [];
 | 
			
		||||
        foreach ($this->values as $col => $value)
 | 
			
		||||
            $setParts[] = "$col = $value";
 | 
			
		||||
@@ -57,10 +44,6 @@ class Update
 | 
			
		||||
        return $update;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Executes the SQL UPDATE query.
 | 
			
		||||
     * @return bool Returns true on success.
 | 
			
		||||
    */
 | 
			
		||||
    public function update(): bool {
 | 
			
		||||
        global $wpdb;
 | 
			
		||||
        $query = $this->toSql();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user