Friday, August 24, 2018

Return a unique and sorted string from number

I have an input which is a number of base 10: 1, 2, 3, 52, 53 ...

I would like to convert the number to a string built with the letters a to z. For each number there should be only one letter combination and they should be sorted alphabetically.

0 => A
1 => B
2 => C
26 => AA
27 => AB
52 => BA
53 => BB
...

At the moment I build this snippet:

var possibleIndexes = "abcdefghijklmnopqrstuvwxyz".split("");
var result = '';
var index10 = 52;
var index26 = (index10).toString(26);

for (var i = 0; i < index26.length ; i++) {
    result += possibleIndexes[(parseInt(index26[i], 26)) % 26];
}

console.log(result);

It's not so far from the correct answer but it's still wrong.

What would be the correct form of the function?

Solved

I generalized the solution and I provided some data for test:

function f(xx) {
    var v = "abcdefghijklmnopqrstuvwxyz".split("");
    var result = '';
    var nr = Math.floor(xx/26);
    var mod = xx%26;

    for ( var j = 1, jj = 1 ; j <= nr ; j=Math.pow(26,jj)+1, jj++ ) {
        result += v[(nr-1)%26];
    }

    result += v[mod];

    return result;
}

/* USEFUL FOR TESTS */
var arr = [0, 1, 25, 26, 52, 53, 701, 702, 17601, 17602, 457001, 457002];
var expected = ['a','b','z','aa', 'ba', 'bb', 'zz', 'aaa', 'zzz', 'aaaa', 'zzzz', 'aaaaa'];
for ( var k = 0 ; k < arr.length ; k++ ) {
    console.log(arr[k] + " --> " + f(arr[k]) + "-->" + expected[k]);
}

Idea:

Take into account that the number of possible solutions grow exponentially related to the length of the output string:

 - A-Z            +26^1            1-26
 - AA-ZZ          +26^2            27-702
 - AAA-ZZZ        +26^3            703-17602
 - AAAA-ZZZZ      +26^4            17603-457002
 ...

Details:

mod keeps the latest character for the output string

j grows exponentially ( 26^0, 26^1, 26^2 ... ) and it assures that result will have the suitable number of characters:

26^1+1 - 26^2   -> add one letter
26^2+1 - 26^3   -> add another letter
...

jj is used only as an exponent ( 1 - 26^1, 2 - 26^2, 3 - 26^3, ... )


Monday, August 20, 2018

Why setUserId method not work in entity symfony2?

Why my methid setUserId not work? It's my Post entity:

id;
    }

    /**
     * Set user_id
     *
     * @param integer $userId
     * @return Post
     */
    public function setUserId($userId)
    {
        $this->user_id = $userId;

        return $this;
    }

    /**
     * Get user_id
     *
     * @return integer 
     */
    public function getUserId()
    {
        return $this->user_id;
    }

    public function getText()
    {
        return $this->text;
    }

    public function __construct() {
        $this->date = new \DateTime();
    }
    //...

}

And my User entity:

posts = new ArrayCollection();

    }


}

I'm saving in the database via my controller:

 public function createAction(Request $request)
    {
        $post = new Post();

        $form = $this->createFormBuilder($post)
                     ->add('text')
                     ->add('address')
                     ->getForm();
        $post->setUserId($this->getUser()->getId());

        $form->handleRequest($request);

        if($form->isValid()){
            $em = $this->getDoctrine()->getManager();
            $em->persist($post);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('home'));    
    }

And i throw this error:

Integrity constraint violation: 1048 Column 'user_id' cannot be null Why? My $this->getUser()->getId() is not null, i tried return new Response($this->getUser()->getId()) and get my id

Solved

You dont need user_id field because you have user relation on field:

 /**
 * @ORM\ManyToOne(targetEntity="Acme\PostBundle\Entity\User", inversedBy="posts")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $user;
/**

Look in you database, doctrine already created user_id for you.


As the others stated its not possible to set $userId on its own if you have also defined an $user property as entity using the same database column.

Instead change your setters and keep everything else as it is:

class Post
{
    // ...

    /**
     * Set user_id
     *
     * @param integer $userId
     * @throws \Exception
     */
    public function setUserId($userId)
    {
        throw new \Exception('Post->userId can not be set directly');
    }

    /**
     * Set user
     *
     * @param User $user
     * @return Post
     */
    public function setUser($user)
    {
        $this->user    = $user;
        $this->user_id = $user->getId();

        return $this;
    }

    // ...
}

Now the $userId is automatically updated when you use setUser(...). Throwing an exception in setUserId helps you preventing bugs in your code. Of course you could also just delete the setter, but it then would be recreated everytime you run another doctrine:generate:entities for your post entity.


Sunday, August 19, 2018

How can I target the tbody of a specific Div?

I have a table that I'd like to target with CSS. The table is inside a div called inner1b

inner1b.tbody{
display:block;
height:800px;
overflow:auto;
}

doesn't seem to be working

Solved

inner1b does not have a . or a #, so your rule is looking for , which isn't a valid HTML tag. You need to do

.inner1b table tbody {
...
}

.inner1b tbody { ... }

Your mistake was connecting the two selectors, which implies a single element. The dot was also out of place.




Saturday, August 18, 2018

How do I save an ERD as a .txp file in Toad for Oracle?

How do I save an ERD in Toad for Oracle as a .txp file or any other format that I can open in Toad Data Modeler?

I have tried to find a way to open the .erx files I have in TDM but it doesn't work that way.

Thank you

Solved

I have been looking for a way to do this for a very long time (about a year now) and it would appear that it just cannot happen.


Import Toad for Oracle ER Diagrams